Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Directory listing

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Postby Joel » Sat Jun 21, 2003 7:14 pm

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

Code: Select all
 <?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>";

?>
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby Redcircle » Sun Jun 22, 2003 10:39 pm

this is my read in directory function

Code: Select all
   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: Select all
$arr = read_in_dir("/home/username/www/images","file");


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

Code: Select all
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 "
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron