Listing the Contents of a Directory With PHP

Following is a quick tutorial explaining how to list the contents of a directory with PHP. Much of this information is taken from the official PHP documentation of the opendir() function.

To begin with, you need to determine which directory you want to list. Start by storing the location of that directory in a variable. If you want to list the contents of the directory in which your script is running, you can use the realpath function to find the full path to that directory. That would look something like:
$dir = realpath('./');
The function above will find the full, absolute path to the current directory and store it as the “$dir” variable.

Next, write yourself a function that looks similar to:
define('HOMEPATH','/home/user/directory'); // Change this to match the absolute path to the root of your Web site (no trailing slash)
define('HOMEURL','http://example.com'); // Change this to match the URL to the root of your Web site (no trailing slash)

function listdir($dir) {
$list = array();
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file !== '.' && $file !== '..' && $file !== 'index.php') {
if(is_dir($dir.'/'.$file)) {
$tmparr = listdir($dir.'/'.$file);
if(count($tmparr)) {
$list[] = '<li><strong>'.$file.'</strong><ul>'.implode('',$tmparr).'</ul></li>'."\n";
}
unset($tmparr);
}
else {
$list[] = '<li><a href="'.str_replace(array(HOMEPATH,'&',' '),array(HOMEURL,'&amp;','%20'),$dir.'/'.$file).'">'.str_replace(array('&'),array('&amp;'),$file).'</a></li>'."\n";
}
}
}
closedir($dh);
}
}
sort($list);
return $list;
}

The function above will recursively search the directory and create an unordered list of links to each of the files found inside. It will ignore “index.php” in each of those directories.

To run the function and print the list onto your screen, you would use code similar to:
$list = listdir($dir);
if(count($list)) { // Check to make sure the list returned had at least one item
echo '<ul>'.implode('',$list).'</ul>';
}

That’s about all there is to it. I hope that helps someone.

Edit: On July 14, 2009, I updated the code in this post due to a potential bug. The code used in this post has been tested and is available in a ZIP file in this post.

6 Responses

  • Riezal

    how about if the directory source is located in byethost but I want to display the list in my site on bplaced.net host?

    • This is not an easy task if the files are hosted on another server. Basically, the byethost server needs to be set up just right in order for you to be able to do something like this. There are basically two options, the way I see it:
      1) If you are able to run PHP scripts on the byethost server, you can add a script to that server that will list the directory, then “include” it with a PHP script on your bplaced server. That, though, will require the byethost server to be configured to allow_url_fopen (I believe)
      2) If you are able to access both servers through SSH, you could potentially mount the byethost server to a local directory on your bplaced server, then work with it that way. That will require a good bit of access to your bplaced server, though, which is normally only found in dedicated server packages, as you will need to be able to edit files owned by root.

      Neither is ideal, unfortunately.

  • Riezal

    You are the master, can you be my teacher, I want to learn PHP with you, may you teach me?

    I have try to display the list of my file in the bplaced on bplaced too,but I got this error message
    Parse error: syntax error, unexpected T_FUNCTION in /users/riezal1992/www/data/index.php on line 5
    what’s wrong?I has just copy your script. . .

    • I have updated the post to include some slightly edited code and a link to an example PHP file for you. I hope that helps.

  • Lemiant

    Why did you replace array(‘&’) with array(‘&’)?

    • It seems that WordPress decoded my characters. It’s supposed to be the HTML encoded equivalent of an ampersand. If things don’t get re-encoded or decoded, the second instance should be &amp; I’ve fixed the post. However, the ZIP file linked in the post will give you a much more accurate picture of what’s supposed to be in the code.

Post Your Comment

Your email address will not be published.