We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

gux on 08/16/06


Tagged

php list directory


Versions (?)


Who likes this?

11 people have marked this snippet as a favorite

dojob
mdavie
blakeb
banjomamo
vali29
hudge
jonhenshaw
JimiJay
MoFu
romanos
carlosabargues


Directory List


Published in: PHP 


URL: http://www.spoono.com/php/tutorials/tutorial.php?id=10

A simple script that lists all the items in the directory including folders and files and even makes a link out of them. Its an all purpose script which you can use to do other things.

  1. //define the path as relative
  2. $path = "/home/yoursite/public_html/whatever";
  3.  
  4. //using the opendir function
  5. $dir_handle = @opendir($path) or die("Unable to open $path");
  6.  
  7. echo "Directory Listing of $path<br/>";
  8.  
  9. //running the while loop
  10. while ($file = readdir($dir_handle))
  11. {
  12. //encode spaces
  13. $file = rawurlencode($file);
  14. // convert the + (this is one result from the function rawurlencode) in %20
  15. $url = str_replace('+' , '%20' , $file);
  16. echo "<a href='".$url."'>".$url."</a><br/>";
  17. }
  18.  
  19. //closing the directory
  20. closedir($dir_handle);

Report this snippet 

You need to login to post a comment.