By 05/02/2014 13 Comments

How to Directory Listing in PHP

What is directory listing ?

  • We can view all type of files which resides in any folder or directory
  • We can read that file’s information along with it’s extensions easily
  • For instance i want to display image files information which is resides in image directory then the following code helps me

Code snippet for directory listing in PHP.

<?php

function list_files($dir)

{

if(is_dir($dir)) //checks if the given file is directory or not, pass one parameter which is the name of a file

{

if($handle = opendir($dir)) // This function open given directory and assign into $handle variable

{

while(($file = readdir($handle)) !== false) // Loop through each item, the function inside loop reads next item from the directory and assign these items in $file variable

{

if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/) // check here if that item is not equal to specified strings in if condition

{

echo ''.$file.'
'."\n";

}

}

closedir($handle); // This function close that open directory

}

}

}

/*

In order to put this to use,

list_files("images/"); //Caller of above function and pass directory name as a argument

?>

*/

?>






← Previous Tutorial


Posted in: PHP

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .