note 22201 deleted from function.filesize by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 01 Sep 2004 17:34:02 +0000 |
| Subject: | note 22201 deleted from function.filesize by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75886@lists.php.net to get a copy of this message | ||
Note Submitter: sponger
----
This may come in handy for someone. Returns the size of the passed file in the appropriate
measurement format.
function my_filesize($file) {
// First check if the file exists.
if(!is_file("./".$file)) exit("File does not exist!");
// Setup some common file size measurements.
$kb = 1024; // Kilobyte
$mb = 1048576; // Megabyte
$gb = 1073741824; // Gigabyte
$tb = 1099511627776;// Terabyte
// Get the file size in bytes.
$size = filesize($file);
/* If it's less than a kb we just return the size, otherwise we keep going until
the size is in the appropriate measurement range. */
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";
}
}