Get Directory Listing

Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Get Directory Listing

 
0
  #1
Dec 18th, 2007
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:
  1. <?php
  2. function showDirectory( $directory )
  3. {
  4. echo("<BR>TESTING: ARG=". $directory);
  5. $dir = opendir( $directory );
  6. if ( $dir )
  7. {
  8. echo("<BR>TESTING: dir has been opened");
  9. while ( false !== ( $file = readdir( $dir ) ) )
  10. {
  11. echo("<BR>TESTING: file=" . $file);
  12. if ( $file != "." && $file != ".." )
  13. {
  14. echo("<BR>TESTING: file IS NOT . or ..");
  15. if (is_dir($directory . "/" . $file))
  16. {
  17. echo("<BR>TESTING: " . $directory . "/" . $file . "is a directory");
  18. showDirectory( $directory . "/" . $file );
  19. }
  20. else
  21. {
  22. echo("<BR>TESTING: " . $directory . "/" . $file . "is NOT a directory");
  23. echo( "<B>" . $directory . "/" . $file . "</B><BR>\n" );
  24. }
  25. }
  26. else
  27. {
  28. echo("<BR>TESTING: file=. OR ..");
  29. }
  30. closedir( $dir );
  31. }
  32. }
  33. else
  34. {
  35. echo("TESTING: $dir = null");
  36. }
  37. }
  38. ?>

Someone else's function #2
  1. <?php
  2.  
  3. function getDirectory( $path = '.', $level = 0 ){
  4.  
  5. $ignore = array( 'cgi-bin', '.', '..' );
  6. // Directories to ignore when listing output. Many hosts
  7. // will deny PHP access to the cgi-bin.
  8.  
  9. $dh = @opendir( $path );
  10. // Open the directory to the handle $dh
  11.  
  12. while( false !== ( $file = readdir( $dh ) ) ){
  13. // Loop through the directory
  14.  
  15. if( !in_array( $file, $ignore ) ){
  16. // Check that this file is not to be ignored
  17.  
  18. $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
  19. // Just to add spacing to the list, to better
  20. // show the directory tree.
  21.  
  22. if( is_dir( "$path/$file" ) ){
  23. // Its a directory, so we need to keep reading down...
  24.  
  25. echo "<strong>$spaces $file</strong><br />";
  26. getDirectory( "$path/$file", ($level+1) );
  27. // Re-call this same function but on a new directory.
  28. // this is what makes function recursive.
  29.  
  30. } else {
  31.  
  32. echo "$spaces $file<br />";
  33. // Just print out the filename
  34.  
  35. }
  36.  
  37. }
  38.  
  39. }
  40.  
  41. closedir( $dh );
  42. // Close the directory handle
  43.  
  44. }
  45.  
  46. ?>
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Get Directory Listing

 
0
  #2
Dec 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Get Directory Listing

 
0
  #3
Dec 18th, 2007
sorry about that, the only error I get is:

  1. Warning: readdir(): 41 is not a valid Directory resource in C:\Program Files\Apache Group\Apache2\htdocs\index.php on line 24
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Get Directory Listing

 
0
  #4
Dec 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Get Directory Listing

 
0
  #5
Dec 19th, 2007
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..

  1. TESTING: ARG=..
  2. TESTING: dir has been opened
  3. TESTING: file=.
  4. TESTING: file=. OR ..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Get Directory Listing

 
0
  #6
Dec 19th, 2007
Nevermind, I have found the mistake.. I was using closedir() inside the loop where I should have been using it outside the loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: jean3167 is an unknown quantity at this point 
Solved Threads: 0
jean3167 jean3167 is offline Offline
Newbie Poster

Re: Get Directory Listing

 
0
  #7
Sep 24th, 2009
Use Print directory to print the directory listing to a file, i found it on google search...
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC