note 28686 added to function.filesize
| From: | brodie-php at tmts dot ca | Date: | Mon, 20 Jan 2003 22:20:42 +0000 |
| Subject: | note 28686 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42445@lists.php.net to get a copy of this message | ||
The following code builds on the some of the above comments. It reads a directory for a particular
file type (.mp3) and then lists the files and their sizes.
In this case, I wanted to put some special mp3s in a directory and be able to add to the list easily
without changing the webpage.
<?php
//$imagedir is the absolute path to the directory wherein you wish to store special mp3s
//example: $imagedir = "/usr/local/www/data/user/dir";
$imagedir = "C:/Windows/User/Dir"; //current directory
$i = 1;
if ($handle = opendir("$imagedir")) {
while (false !== ($file = readdir($handle))) {
//strncmp returns < 0 if arg1 is less, >0 if more, 0 if equal
$extension = substr($file, -4, 4);
if ($file != "." && $file != ".." && ($extension ==
".mp3")) {
echo($i++.". <a href=\"$file\">$file
(".fsize($file).")</a><br>");
}
}
closedir($handle);
}else{
echo("<br>Directory could not be opened.<br>");
}
function fsize($file) {
// Does the file exist?
if(is_file($imagedir.$file)){
//Setup some common file size measurements.
$kb=1024;
$mb=1048576;
$gb=1073741824;
$tb=1099511627776;
//Get the file size in bytes.
$size = filesize($file);
//Format file size
if($size < $kb) {
return $size." B";
}
else if($size < $mb) {
return round($size/$kb,2)." KB";
}
else if($size < $gb) {
return round($size/$mb,2)." MB";
}
else if($size < $tb) {
return round($size/$gb,2)." GB";
}
else {
return round($size/$tb,2)." TB";
}
}
}
?>
--
http://www.php.net/manual/en/function.filesize.php
http://master.php.net/manage/user-notes.php?action=edit+28686
http://master.php.net/manage/user-notes.php?action=delete+28686
http://master.php.net/manage/user-notes.php?action=reject+28686