keepcalculator

06 Numbers and Powers

List PHP Files in a Folder with Meta Descriptions

Card No. 06·21402 · Updated · Standard formula, shown below

This guide shows you how to automatically list all PHP files in a folder on your server and display each one with its own meta description. You’ll end up with a clean index page that links to every script, pulling the description from a small text file or from the PHP files themselves. This is useful for documentation pages, tool directories, or simple file managers. You’ll need basic knowledge of PHP and file permissions, plus access to your server’s file system.

What You’ll Need

  • A web server with PHP installed (version 7.0 or newer recommended).
  • Write access to the folder where your PHP files are stored.
  • A text editor to create or modify PHP files.
  • Basic understanding of PHP arrays and loops.

Step-by-Step

  1. Create a folder for your PHP files. If you don’t already have one, make a directory on your server, for example /public_html/tools/. Place the PHP files you want to list inside this folder. For this guide, we’ll call it scripts/.
  2. Create a meta descriptions file. In the same folder (or a safe location above it), create a plain text file named descriptions.txt. Each line should contain the filename and the description separated by a pipe character |. For example:
    calculator.php|A simple arithmetic calculator with history.
    converter.php|Convert between metric and imperial units.
    Make sure the filename matches exactly, including case and extension.
  3. Write the PHP script to read the folder. Create a new file called index.php in the same folder. Open it with your text editor and start with the opening PHP tag. Use the scandir() function to get an array of all files in the current directory. Exclude the current file and any hidden files (those starting with a dot).
  4. Parse the descriptions file. Use file() to read descriptions.txt into an array of lines. Loop through each line, split it on the pipe character using explode(), and store the result in an associative array where the key is the filename and the value is the description. This makes it easy to look up a description later.
  5. Filter to PHP files only. Loop through the array returned by scandir(). For each item, check if it’s a file (not a directory) using is_file() and if it ends with .php using pathinfo() or substr(). Skip the current script (index.php) and any other files you don’t want to list.
  6. Build the HTML list. For each PHP file that passes the filter, create a list item. The link href should be the filename itself. The link text can be the filename without the .php extension, or you can use a nicer name from a separate array if you have one. Inside the list item, add a paragraph or a span with the meta description. If no description exists for that file, display a default message like “No description available.”
  7. Sort the list alphabetically. Use sort() on the array of filenames before generating the HTML. This makes the page easier to scan. If you want to sort by description or by a custom order, you can modify the array accordingly.
  8. Output the page. Wrap the list in a proper HTML structure with a <ul> or a <table>. Add a heading like “Available Scripts” and a short intro paragraph. Use htmlspecialchars() on any output that comes from the file system to prevent XSS attacks if a filename contains special characters.
  9. Test the page. Open your browser and navigate to the folder where index.php is located (e.g., https://yoursite.com/tools/). You should see a list of all PHP files with their descriptions. Click a few links to make sure they work.
  10. Handle empty folders. If the folder has no PHP files, display a friendly message like “No scripts found.” instead of an empty list. Check the count of the filtered array before generating the HTML.

Common Mistakes

  • Forgetting to exclude the index.php file itself. This causes the page to list itself, which is usually not desired. Always skip the current script.
  • Incorrect file permissions. If descriptions.txt is not readable by the web server, the script will fail silently or show warnings. Set permissions to 644 for files and 755 for folders.
  • Mismatched filenames. The descriptions file must have the exact filename including case. On Linux, Calculator.php and calculator.php are different files.
  • Not using htmlspecialchars(). If a filename contains an ampersand or a quote, it can break the HTML. Always escape output.
  • Listing subdirectories as files. Use is_file() to ensure you only list files, not folders.

Tips

  • Use a separate array for display names. If your filenames are cryptic like calc_v2.php, create a names.php file with a mapping to friendly titles.
  • Cache the description file. If you have many files, reading and parsing the text file on every request is fine for small lists, but for hundreds of files, consider caching the parsed array in a static variable or a session.
  • Add a search box. You can easily add a client-side filter using JavaScript to let users search through the list without reloading the page.
  • Include file modification dates. Use filemtime() to display when each script was last updated, which is helpful for documentation.
  • Make the descriptions file optional. If the file doesn’t exist, the script should still list all PHP files with a generic description instead of failing.

Troubleshooting

  • Page shows a blank white screen. This usually means a PHP error. Enable error reporting by adding error_reporting(E_ALL); ini_set('display_errors', 1); at the top of the script to see the error message. Common issues are missing semicolons or undefined array keys.
  • No files are listed. Check that the folder path is correct. If you used a relative path, ensure the script is in the same folder. Also verify that the files have the .php extension and that the folder is not empty.
  • Descriptions are not showing. Make sure the descriptions.txt file is in the correct location and that the format is exactly filename|description with no spaces around the pipe. Also check that the filename in the text file matches the actual filename.
  • Links point to the wrong location. If your PHP files are in a subfolder, the href should include the subfolder path. Use a relative path from the current script or an absolute path based on a defined base URL.
  • Warning: scandir() expects parameter 1 to be a valid path. This means the folder path is wrong. Use __DIR__ to get the current script’s directory, then append the folder name: $dir = __DIR__ . '/scripts';
  • Special characters in filenames break the HTML. Always run filenames through htmlspecialchars() before outputting them in href or text.

Frequently Asked Questions

Can I list PHP files from multiple folders?

Yes, you can use scandir() on each folder and merge the arrays. Or use RecursiveDirectoryIterator to get files from subfolders, but then you’ll need to adjust the href to include the relative path.

How do I add a meta description to each link for SEO?

The meta description in the HTML head is for the page itself, not for individual links. To give each linked page its own meta description, you need to edit each PHP file’s <head> section. This script only displays a description next to the link, which is not a meta tag.

What if I don’t want to create a separate descriptions file?

You can parse the PHP files themselves for a @description comment in the header. Use file_get_contents() and a regular expression to extract it. This is more automated but slower for many files.

Can I exclude certain PHP files from the list?

Yes, create an array of filenames to exclude and check against it in the loop. For example, exclude config.php or functions.php.

How do I make the list look nicer with CSS?

Wrap each item in a <div> with a class, then add CSS to style the links and descriptions. You can also use a table with columns for name, description, and date.

Is this safe to use on a public server?

Yes, as long as you escape output and don’t allow user input to directly control file paths. The script only reads the folder and outputs links, so it’s safe. However, ensure the folder doesn’t contain sensitive files that you don’t want listed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →