HOME       >>       Programming

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


Corey

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


Ao)K-General

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.


yasir

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


HmmZ

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 )

bjrn

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

mobious

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


iGuest

Good Php Directory

Getting List Of Directories And Files Using Php

 

Here is a good PR4 Php directory

 

http://www.businessservicesuk.com/

 

-reply by Pete


iGuest

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


iGuest

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




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


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

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.