Listing files stored within a directory using PHP
Tuesday, July 8th, 2008 at 6:23pm, in File Management, written by Tom Foyster
Being able to create lists out of files within a directory quickly and easily is a really good skill to have, especially when creating Content Management and web based File Management Systems.
This method is a really simple version, and can be expanded in a number of ways.
Firstly we create a function to return the formatted file list. When the function is called, we also send through the directory to be used as a string.
function file_list($dir) {
}
We then create a variable to hold our formatted list.
function file_list($dir) {
$list = ;
}
Then, we need to start off our html list.
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
}
Next we need to create a variable to hold our opened directory. To do this we use the opendir() function.
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$open_file = opendir($dir);
}
Now we have the directory open, we can loop through its contents.
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$opened_file = opendir($dir);
while ( $file = readdir($opened_file) ) {
}
}
When we loop through the file, we need to check that the $file variable isnt this directory or its parent. These are represented by . and ...
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$opened_file = opendir($dir);
while ( $file = readdir($opened_file) ) {
if ( $file != . && $file != .. ) {
}
}
}
Now we can enter the files into our list, within the loop.
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$opened_file = opendir($dir);
while ( $file = readdir($opened_file) ) {
if ( $file != . && $file != .. ) {
$list .= <li> . $file . </li> . \r\n;
}
}
}
Then we need to close off our list after the while loop has finished.
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$opened_file = opendir($dir);
while ( $file = readdir($opened_file) ) {
if ( $file != . && $file != .. ) {
$list .= <li> . $file . </li> . \r\n;
}
}
$list .= <ul> . \r\n;
}
Then, just to be tidy, we close the open directory, stored in the $opened_file variable. Also, remember to return the finished list!
function file_list($dir) {
$list = ;
$list .= <ul> . \r\n;
$opened_file = opendir($dir);
while ( $file = readdir($opened_file) ) {
if ( $file != . && $file != .. ) {
$list .= <li> . $file . </li> . \r\n;
}
}
$list .= </ul> . \r\n;
closedir($opened_file);
return $list;
}
Finally, just call the function, passing through the directory you want to list.
$files = file_list(images/);
Share and Enjoy:
Subscribe to Total PHP: RSS | Email
Related Posts:
- Creating a text or csv file of information from your database
Recommended resources
Recent articles
- How to Read an RSS Feed with PHP 5
- Performing searches on strings using strpos
- PHP form validation basics
- Using a global configuration file
- Creating a text or csv file of information from your database
- Using an autoload function to make your life easier
- Choosing a PHP Web Host
- Why you will love PHP5

















Comments on this article
By chancegarcia, Thursday, August 7th, 2008 at 12:32pm
bah. sorry. here's a html friendly display format.
function listing($dir) {
$doNotWant=array(".","..");
$listing="
";- $file
";
";$files=scandir($dir);
foreach($files as $file) {
if (!is_dir($file)&&!in_array($file,$doNotWant)) {
$listing.="
}
}
$listing.="
return $listing;
}
By chancegarcia, Thursday, August 7th, 2008 at 12:27pm
wouldn't this be a bit cleaner?
function listing($dir) {
$doNotWant=array(".","..");
$listing="
";- $file
";
";$files=scandir($dir);
foreach($files as $file) {
if (!is_dir($file)&&!in_array($file,$doNotWant)) {
$listing.="
}
}
$listing.="
return $listing;
}
I use something along these lines for the same tasks. also, with either method, you can go recursive with is_dir()
Nice tutorial though.
By urbiance, Thursday, July 17th, 2008 at 9:02pm
Nice explanation, thanks.
Please log in to post a comment about this article.