Software Developer

Display All Files in a Directory With PHP

This is raw php code that will dynamically generate a list of all files in a directory so that you can view this list online. This is useful if you have a website with a random private directory in which you like to store private files. Before using this code, you should have some type of security in place to prevent the public from accessing your directory (for example, with .htacess). All I’m doing here is giving you the script to let you view the list of these files online. This list will be clickable; clicking on the filename will open the file.

Step 1

Create a new, blank file in the directory. Name the file index.php.

Step 2

Paste the following PHP code into the the blank index.php.

<?php
 if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
            $thelist .= '<a href="'.$file.'"><br />'.$file.'</a>';
          }
       }
  closedir($handle);
  }
?>
<p>List of files:</p>
<p><?=$thelist?></p>

Note:
This list is not styled with any CSS styles. It’s just a plain page with a list of your files. This is meant to help you keep your files organized, not for making an attractive web page. You can get creative with this by styling your own HTML page, then inserting this PHP code into the body of the html page.

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]