| | |
Get Directory Listing
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
Hey, I am trying to recursively retrieve the directory listing of a given directory that is, print out all files and folders (and sub folders) of a given directory. My function #1 does not work, however the function someone else has written #2 does. Why is this?
My Function #1:
Someone else's function #2
My Function #1:
php Syntax (Toggle Plain Text)
<?php function showDirectory( $directory ) { echo("<BR>TESTING: ARG=". $directory); $dir = opendir( $directory ); if ( $dir ) { echo("<BR>TESTING: dir has been opened"); while ( false !== ( $file = readdir( $dir ) ) ) { echo("<BR>TESTING: file=" . $file); if ( $file != "." && $file != ".." ) { echo("<BR>TESTING: file IS NOT . or .."); if (is_dir($directory . "/" . $file)) { echo("<BR>TESTING: " . $directory . "/" . $file . "is a directory"); showDirectory( $directory . "/" . $file ); } else { echo("<BR>TESTING: " . $directory . "/" . $file . "is NOT a directory"); echo( "<B>" . $directory . "/" . $file . "</B><BR>\n" ); } } else { echo("<BR>TESTING: file=. OR .."); } closedir( $dir ); } } else { echo("TESTING: $dir = null"); } } ?>
Someone else's function #2
php Syntax (Toggle Plain Text)
<?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces $file<br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?>
Please elaborate on "does not work", does it give errors or just not print anything?
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
sorry about that, the only error I get is:
PHP Syntax (Toggle Plain Text)
Warning: readdir(): 41 is not a valid Directory resource in C:\Program Files\Apache Group\Apache2\htdocs\index.php on line 24
The only thing I can tell is that the second function is suppressing warnings/errors when it opens a directory with the @
$dh = @opendir( $path ); as opposed to $dir = opendir( $directory );. GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
I have tried the @ to suppress warnings on all the functions I am using, I don't get any messages from PHP but now all my function displays are the testing echos..
PHP Syntax (Toggle Plain Text)
TESTING: ARG=.. TESTING: dir has been opened TESTING: file=. TESTING: file=. OR ..
•
•
Join Date: Sep 2009
Posts: 3
Reputation:
Solved Threads: 0
Use Print directory to print the directory listing to a file, i found it on google search...
![]() |
Similar Threads
- Slow directory listing (Networking Hardware Configuration)
- Redirecting a Directory or page... (Linux Servers and Apache)
- Need help with directory displayer code (PHP)
- Yahoo! directory listing (Search Engine Optimization)
Other Threads in the PHP Forum
- Previous Thread: PHP Upload
- Next Thread: Holding a users ID and pwd during a session
| Thread Tools | Search this Thread |
301 advanced apache api array autosuggest beginner beneath broadband broken button cakephp class cms code compression countingeverycharactersfromastring crack cron curl data database date decode display dropdownlist dynamic email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include integration ip javascript joomla jquery key library limit link login match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript protocol query random screen script search searchbox server session sessions smtp source space sql strip_tags survey system table tutorial upload url variable video virus volume votedown web website window.onbeforeunload=closeme; youtube zend






