Scripts and functions that I have written or found useful

PHP Listing files in a folder

September 6th, 2006 Posted in PHP

This script goes through a given folder and finds all of the files and folders within. Using this script it is possible to recursively open up folders.

PHP:
  1. $PATH = $_ENV['DOCUMENT_ROOT'];
  2.  
  3. $d=$PATH.'/home/server/public_html/folder/';
  4.  
  5. $files=array(); #initialize array
  6.  
  7. $dir = opendir($d);
  8. while ($f = readdir($dir)) {
  9. if (eregi("\.php",$f)){ #if filename matches .txt in the name
  10. array_push($files,"$f"); #push into $files array
  11. }
  12. }
  13. $count = count($files);
  14. print ":".$d;
  15. for($x=0;$x<$count;$x++){
  16.  
  17. $x: ".$files[$x];
  18. }

Post a Comment