About MeMiscellaneousGeek StuffSkydiving |
Geek Stuff : Tutorials : PHP : Display Files in a Directory with PHPSometimes it can be useful to display the files in a directory. This can be especially useful in a content management system. It's not hard to do, and the code follows. The comments explain what the code is doing. <?php // first we open the directory and assign it a file handle if($dirHandle = opendir($_SERVER['DOCUMENT_ROOT'] . "/path/to/dir/")) { echo "<p>\n"; // loop through all the files in the directory while($file = readdir($dirHandle)) { // strip out the directory . and .. directory stuff if($file != "." && $file != "..") { // then display a link to each file in the directory on the page echo "<a href=\"/path/to/dir/{$file}\">{$file}</a><br/>\n"; } } echo "</p>\n"; // release the directory handle when we're done with it closedir($dirHandle); } ?> Basically we opened the directory and assigned it to a handle. Then we looped through the file handle and displayed a link to each file (except the UNIX directory navigation . and .. items) on the page. You could get more elaborate with this and put the files in an HTML table and include links to delete items too (for another tutorial, check back soon!) |
|
| ©1995 - 2008 Tim Patterson, All Rights Reserved (Unless otherwise noted) | ||