note 77518 modified in function.filesize by danbrown
| From: | danbrown@php.net | Date: | Sun, 18 Jan 2009 18:04:47 +0000 |
| Subject: | note 77518 modified in function.filesize by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149403@lists.php.net to get a copy of this message | ||
Updated function to return human readable file size:
<?php
function size_readable ($size, $retstring = null) {
// adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
$sizes = array('B', 'kB', 'MB', 'GB',
'TB', 'PB', 'EB', 'ZB', 'YB');
if ($retstring === null) { $retstring = '%01.2f %s'; }
$lastsizestring = end($sizes);
foreach ($sizes as $sizestring) {
if ($size < 1024) { break; }
if ($sizestring != $lastsizestring) { $size /= 1024; }
}
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't
normally fractional
return sprintf($retstring, $size, $sizestring);
}
?>
--was--
Updated function to return human readable file size:
function size_readable ($size, $retstring = null) {
// adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
$sizes = array('B', 'kB', 'MB', 'GB',
'TB', 'PB', 'EB', 'ZB', 'YB');
if ($retstring === null) { $retstring = '%01.2f %s'; }
$lastsizestring = end($sizes);
foreach ($sizes as $sizestring) {
if ($size < 1024) { break; }
if ($sizestring != $lastsizestring) { $size /= 1024; }
}
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't
normally fractional
return sprintf($retstring, $size, $sizestring);
}
http://php.net/manual/en/function.filesize.php