How to write a PHP coding to list out all files and directories as links to them

This is somewhat similar to some index pages. When new file or folder is added to the directory, HTML page should display the newly created file/folder together with previous ones after it is being refreshed. (prefer in alphabatical order)

Here is the function that recursively scan files and directories within a given directory, and generates an array of all files/directories with their paths. You can then use this array to generate links as you need.

[php]
// Recursively find files in the given directory

function find_files($mydir){

global $results;

if(($tdir=@opendir($mydir))!==false){
  while($f=readdir($tdir)){
    if($f!="." and $f!=".."){
      if(is_dir($mydir."/".$f)){
        // directory found
        if(find_files($mydir."/".$f)>=1000) return count($results);
      }
      elseif(is_file($mydir."/".$f)){
        // file found
        if(count($results)>=1000) return count($results);
        $results[]=$mydir."/".$f;             
      }
    }
  }  
  closedir($tdir);
}
return count($results);

}
[/php]

Here is how to use this function:
[php]<?php
$results = array();
find_files(’…/’);

if(count($results)) foreach($results as $line) echo $line."
\n";
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service