PHP MySQL Tutorial
Learn PHP and MySQL

List a Directory's Contents Using PHP

Page Details

Published by:
admin
on 12-20-2008
This page has not yet been rated

List a Directory's Contents Using PHP

To list the content of a directory you just need to use the combination of opendir() and readdir() functions.

// directory path can be either absolute or relative
$dirPath = '.';

// open the specified directory and check if it's opened successfully
if ($handle = opendir($dirPath)) {

   // keep reading the directory entries 'til the end
   while (false !== ($file = readdir($handle))) {

      // just skip the reference to current and parent directory
      if ($file != "." && $file != "..") {
         if (is_dir("$dirPath/$file")) {
            // found a directory, do something with it?
            echo "[$file]<br>";
         } else {
            // found an ordinary file
            echo "$file<br>";
         }
      }
   }

   // ALWAYS remember to close what you opened
   closedir($handle);
}


When you list the files surely you need to know which of these files are actually a sub-directory. Using the is_dir() function you can test if the files is really a directory or not. What you want to do later with that directory is up to you. On the code above directories are simply printed in a square brackets to differentiate them with ordinary files

Recent Comments

By: TigerDragon Posted on 10-02-2009 5:23 PM

Hello All,

My code is very similar to the original poster. A minor differences on logic and with the commands use, plus I only list *.pdf files but I think it doesn't matter. What bothers me is, the code is being ran on WinXP Pro and on Ubuntu Server. On XP, files are listed alphabetically in order (which is perfect) but on Ubuntu, files are listed random. And I dont know why, Am I missing something on PHP or do I need to configure ubuntu? puzzles me.

I issued a command "dir" and "ls -l or ls -al" on ubuntu prompt and found that both command list it alphabetically. --> confuses me more

Thinking a work around... I can modify PHP to save it to array then sort it before displaying, but not willing to that (yet) if I only miss something very minor. Plus my ubuntu sever has lot more than files than my XP which I believe will be running slower if I do that.

I'm no PHP expert nor linux and needed someone who can point me to right direction.  I just dont know where to start. Any comments by Linux experts??? or comments by PHP experts.

if it matters. i use UBUNTU 8.04 and just updated to PHP 5.2.4 (i guess)

Thank you in advance.

TD

By: TigerDragon Posted on 10-02-2009 5:53 PM

For your reference, heres the PHP portion

<? $path = "/var/www/datasheets/"; //Only difference with the XP version

$itemsNo = 0;

$DirHandler = opendir($path) or die("Unauthorize Access");

print("<td width =\"100%\"><div align=\"center\">");

print("<table border=1 cellpadding=5 cellspacing=0 class=whitelinks>");

print("<tr bgcolor=\"FF0F0F\" ><th>Item No.</th><th>File</th><th>Filesize</th></tr>");

while ($file = readdir($DirHandler)) {

if  (($file !="index.php") && (substr("$file", 0, 1) != ".")) {

$itemNo++;

$ModelNo = substr("$file",0,strlen($file) - 4);

print("<tr><td align=\"center\">$itemNo</td>");;

print("<td><b><a href=\"$file\">$ModelNo</a></b></td>");

print("<td>");

$dispsize = filesize($file);

$UnitSize = " Bytes";

if ($dispSize >= 1000000) {

$dispsize = $dispsize/1000000;

$UnitSize = " MB";

}

if ($dispSize >= 1000) {

$dispsize = $dispsize/1000;

$UnitSize = " KB";

}

print($dispSize));

print($UnitSize);

print("</td></TR>");

}

print("</div></td>");

}

closedir($DirHandler);

?>

I temporary drop the index.php here --> www.johnlauder.com/projects  (this is running under WinXP, the ubuntu is plainly internal)

thanks again.

jL aka TD

By: omg Posted on 03-08-2010 7:19 PM

this is great, thanks alot!

just started using tinygrab, which uploads my screenshots (to a sub domain-folder). would be awsome if i could use http://tiny.trstd.se like a screenshot-blog ^^

should have learned php when the web was young, but i didn't.

can someone plix help me write a script that echoes "<img src=$file><br>"; in the order of creation-date and only if the file is a png?

By: fq0029 Posted on 10-04-2011 4:02 AM

bedspreads

Your site is good Actually, i have seen your post and That was very informative and very entertaining for me. Thanks for posting Really Such Things. I should recommend your site to my friends. Cheers.

By: apudiu1 Posted on 12-17-2011 3:33 AM

Brisbane's Metal Roofing Professionals

Jones Roofing is a family owned business celebrating over 16 years in the Brisbane roofing industry.

Specialising in metal roofs, our services include new roofs, re-roofing, asbestos re-roofing, roof repairs, guttering and ceiling insulation for residential and commercial buildings.

For more please visit:www.jonesroofing.com.au

Powered by Community Server (Non-Commercial Edition), by Telligent Systems