Posted By

gux on 08/16/06


Tagged

php list directory


Versions (?)

Who likes this?

20 people have marked this snippet as a favorite

dojob
mdavie
blakeb
banjomamo
vali29
hudge
jonhenshaw
JimiJay
MoFu
romanos
carlosabargues
irishsk
marnold2000
BK4D
ba11in
bobbym245
mecha
lfcortes
ringo380
clacwebstudio


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  

Comments

RSS Icon Subscribe to comments
Posted By: sb_01 on September 19, 2010

think not really secure u can access sys folder ? and u can use "urlencode" for encoding the url instead of "strreplace"

You need to login to post a comment.