BC Tech : Free Tutorials and More Advertisement
Main arrow Tutorials arrow Programming arrow Generate Dynamic Index Files in PHP
Wednesday, 07 January 2009
 
 
Generate Dynamic Index Files in PHP Print E-mail
Tutorials / FAQs - Programming
Article Index
Generate Dynamic Index Files in PHP
Page 2

 

This script will show you how to traverse the current directory so you can get information about it's files and directories.

 

Create an array with the file info
  1. <?php
  2.   $pathStr = $_SERVER["DOCUMENT_ROOT"].$_SERVER["PHP_SELF"];
  3.   $dir = substr($pathStr, 0, strrpos($pathStr, "/")+1);
  4.   //HTTP_HOST
  5.   $dh  = opendir($dir);
  6.   $aDirArr = array();
  7.   $aFileArr = array();
  8.   while (false !== ($filename = readdir($dh))) {
  9.     $pos = strrpos($filename, ".");
  10.     if (  ( is_dir($filename) && $filename != ".")
  11.         || substr($filename, strrpos($filename, "."), strrpos($filename, ".")+3) == ".mp3"
  12.         || substr($filename, strrpos($filename, "."), strrpos($filename, ".")+3) == ".gif"
  13.         || substr($filename, strrpos($filename, "."), strrpos($filename, ".")+3) == ".jpg"
  14.       ) {
  15.         $nFileKBsize = filesize($filename);
  16.         settype($nFileKBsize, "integer");
  17.         $nFileKBsize = $nFileKBsize/1024;
  18.         settype($nFileKBsize, "integer");
  19.  
  20.         if(is_dir($filename)) {
  21.           $aDirArr[$filename] = array();
  22.           $aDirArr[$filename]['filename'] = $filename;
  23.           $aDirArr[$filename][] = "";
  24.         }
  25.         else {
  26.           $aFileArr[$filename] = array();
  27.           $aFileArr[$filename]['filename'] = $filename;
  28.           if(is_file($filename)) {
  29.             $aFileArr[$filename]['filesize'] = number_format($nFileKBsize)." KB";
  30.           }
  31.           else {
  32.             $aFileArr[$filename]['filesize'] = "";
  33.           }
  34.         }
  35.     }
  36.   }
  37. ?>

 

I'll show you how to generate the output on the next page.



 
< Prev   Next >
Google
 
Web benconley.net
 
Top! Top!