note 29704 added to ref.dir
| From: | admin at csmapcentral dot com | Date: | Sat, 22 Feb 2003 02:33:38 +0000 |
| Subject: | note 29704 added to ref.dir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44336@lists.php.net to get a copy of this message | ||
tlsprwr at hotmail dot com made some code corrections, but since he hadn't tried his
corrections, he didn't realize that he was trying to divide the variable $path by $file
e.g.- ($path/$file)/1024
i corrected this by chging his code to ($path.$file)/1024
here's my version of the code with a couple of additions (a new variable and an if statement in
case a user is using the relative path):
$base_dir='/var/www/html/files/';
//define the path as relative
//$path = ".";
//$base_dir is a variable i created that contains the non-relative dir path
$path = $base_dir;
//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
if ($path != "."){
while ($file = readdir($dir_handle)) {
// filename - output left-justified
$t="<a href=$path/$file>$file</a>";
echo $t.substr($space,0,40-strlen($file)) ;
// filesize - output right-justified
$t=(filesize($path.$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($path.$file));
echo $t.substr($space,0,20-strlen($file)) ;
echo "\n";
}
} elseif ($path = ".") {
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>";
--
http://www.php.net/manual/en/ref.dir.php
http://master.php.net/manage/user-notes.php?action=edit+29704
http://master.php.net/manage/user-notes.php?action=delete+29704
http://master.php.net/manage/user-notes.php?action=reject+29704