Jump to content
xisto Community
Sign in to follow this  
Corey

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

Recommended Posts

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:

include("folder_content.php");

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 :D )

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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";

Share this post


Link to post
Share on other sites

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

/New folder

new.txt
left.gif
download.zip
dc.exe


....so is there..? :D




Hi,

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.... )

Enjoy :)
-Ankita Gupta

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.