HOME       >>       Programming

Getting List Of Directories And Files Using Php PHP Function for Directory and File List


is there a php function that lists the content of some folder....example:/New foldernew.txtleft.gifdownload.zipdc.exe....so is there..?


I have no clue. I think there might be but I dunno. There is something where you can register and the info goes to a folder. But I dunno.


best thing would be to Google it!Try something like 'Advanced PHP Tutorials'Cheers!Yasir


I don't think theres a pre-programmed function for that, you could, of course do it manually, updating it manually when your folder has new content:

<?php $folder_content = 'echo "The following files are content of the selected folder:"echo "$list"';$list= "new.txt, left.gif, download.zip, dc.exe";echo "$folder_content"; ?>

You could put that in a new file called "folder_content.php" and then (for example in your indexpage, put the following line:


That way you can easily edit the content it should display manually, while using minimum bytes for your indexpage.

Hope that works for you (im just a newbie phper )


is there a php function that lists the content of some folder....

There is opendir() which you can use to open a dir and then loop through the contents.
<?phpif ($handle = opendir("yourdirhere")) {  while (($file = readdir($handle)) !== false) {    echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";  }  closedir($dh);}?>
Note that this will also list sub-directories and '.' and '..'. You can get info in the php.net documentation for opendir().


i came up with this. it is similar with bjrn's but will sort all folder and files alphabetically. also will be conatined in a multi-dimensional array together with their name and statistics.

$directory = "path/to/dir";while (($item = $directory->read()) !== false) { if ($item != "." && $item != "..") {  $path = "{$directory->path}/{$item}";    if (is_dir($path)) {   $tmp['name'] = $item;   $tmp['stats'] = lstat($path);     $dirs[$item] = $tmp;     unset($tmp);  } elseif (is_file($path)) {   $tmp['name'] = $item;   $tmp['stats'] = lstat($path);   $tmp['extension'] = substr($item, strrpos($item, "."));     $files[] = $tmp;     unset($tmp);  } }}ksort($dirs, SORT_STRING);sort($dirs);ksort($files, SORT_STRING);sort($files);


Good Php Directory

Getting List Of Directories And Files Using Php


Here is a good PR4 Php directory




-reply by Pete


Here's a little something I whipped up when Google failed to find anything for me.

function get_dirlist( $path, $match = '*', $exclude = array( '.', '..' ) ){ $result = array(); if( ( $handle = opendir( $path ) ) ) { while( false !== ( $fname = readdir( $handle ) ) ) { $skip = false; if( !empty( $exclude ) ) { if( !is_array( $exclude ) ) $skip = fnmatch( $exclude, $fname ); else { foreach( $exclude as $ex ) { if( fnmatch( $ex, $fname ) ) $skip = true; } } } if( !$skip && ( empty( $match ) || fnmatch( $match, $fname ) ) ) $result[] = $fname; } closedir( $handle ); } return $result;}

Simple Usage:

$list = get_dirlist( '.' );foreach( $list as $fname ) echo "<p><a href=\"$fname\">$fname</a></p>\n";

Advanced Usage:

$exclude = array( '.', '..', 'index.php');$list = get_dirlist( '.', '*.php', $exclude );foreach( $list as $fname ) echo "<p><a href=\"$fname\">$fname</a></p>\n";


is there a php function that lists the content of some folder....

/New folder


....so is there..?


I have the simplest code for you, just create a php file, having following code

<?phpfunction getFileList($dir) { // array to hold return value $retval = array(); // add trailing slash if missing if(substr($dir, -1) != "/") $dir .= "/"; // open pointer to directory and read list of files $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading"); while(false !== ($entry = $d->read())) { // skip hidden files if($entry[0] == ".") continue; if(!is_dir("$dir$entry") && is_readable("$dir$entry")) { $retval[] = "$dir$entry"; } } $d->close(); return $retval; } // for example your folder name is img for the time being.. $dirlist = getFileList("img"); echo "<pre>",print_r($dirlist),"</pre>";?>
and execute it... And it will give you an array containing the list of all files of that folder, it simple skips the folders and hidden files.
(Note that you have to put this script in the same folder where your img folder exists.... )

-Ankita Gupta


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.