Directory listing

A

Anonymous

Guest
this is a code I use for listing a directory. I dunno how you could list things by certain criteria, this lists everything.

Code:
 <?php
//define the path as relative
  $path = ".";

  //using the opendir function
  $dir_handle = @opendir($path) or die("Unable to open $path");
  
  //make string with 70 spaces
  $space="                                 ";
  
  echo "<PRE>";
  echo "Directory Listing of $path\n";
  //running the while loop
  
  while ($file = readdir($dir_handle)) {
        
        // filename - output left-justified
        $t="<a href=$file>$file</a>";
        echo $t.substr($space,0,40-strlen($file))  ;
        
        // filesize - output right-justified
        $t=(filesize($file)/1024);
        $t=sprintf("%01.2f",$t)."kb ";
        echo substr($space,0,10-strlen($t)) . $t ;
        
        // filedate - output left-justified
        $t=date("d.M Y H:i:s", filemtime($file));
        echo $t.substr($space,0,20-strlen($file)) ;
        echo "\n";
  }
  //closing the directory
  closedir($dir_handle);
  echo "</PRE>";

?>
 
this is my read in directory function

Code:
	function read_in_dir($changedir, $file_or_dir)
	{
		
		chdir($changedir);
		if ($dir = opendir($changedir))
		{
			$fileArray = array();
			$dirArray = array();
			while($file = readdir($dir))
			{
				
				if($file!=".." && $file!=".")
				{
					if(!(is_dir("$file")))
					{
						
						$fileArray[] = $file;
						
					}
					else
					{
						$dirArray[] = $file;
					}
				}
			}
			closedir($dir);
		}
		if($file_or_dir == 'file')
		return $fileArray;
		if($file_or_dir == 'dir')
		return $dirArray;
		
	}

basically to use this you put in the path on your server and specify wether you want to get directory names or file names.
Code:
$arr = read_in_dir("/home/username/www/images","file");

You would then extract the data you are looking for from the array.

Code:
if(is_array($arr))
{
 echo '<table>';
   foreach($arr as $key=>$value)
  {
    if(preg_match("/photo.*?\.php/i",$value))
    {
    echo '<tr><td>'.$value.'</td></tr>';
     }
  }
  echo '</table>';
}

I'm kinda fuzzy on what you meant by "contents of the 13th table cell "
 
Back
Top