Archive for May 18th, 2006

Using DirectoryIterator to List Files in PHP

During the days of PHP4, the most common way of showing the files in a directory was like so:

PHP:
  1. if ($handle = opendir('/home/fernando/temp')) {
  2.    while (false !== ($file = readdir($handle))) {
  3.        if ($file != "." && $file != "..") {
  4.            print "$file <br />";
  5.        }
  6.    }
  7.    closedir($handle);
  8. }

However, if your PHP version has now been upgraded to PHP 5, you can take advantage of DirectoryIterator instead. This class (don't get scared) is part of the SPL library that is now bundled within PHP 5.

10 comments May 18th, 2006


Calendar

May 2006
M T W T F S S
« Apr   Jun »
1234567
891011121314
15161718192021
22232425262728
293031  

Posts by Month

Posts by Category