/ Published in: PHP
Simply pass the path to the file, the desired units of measurement, decimal rounding, and separator. For example, a 1024kb file (1mb)
$filesizeStats = getFilesize("path/to/file.jpg", "kb", 2, " ");
print_r($filesizeStats);
//will return
array (
"rawBytes" => 131072,
"convertedSize" => 1024.00,
"convertedSize_formatted" => "1,024.00",
"units" => "kb",
"formatted" => "1,024.00 kb"
)
Expand |
Embed | Plain Text
/** * This method calculates, converts, and reports filesize information * @access public * @author Matt Ford * @param string $file absolute path to a file * @param string $units abbreviation of units to report formatted size in, defaults to kb * @param integer $rounding number of decimal places to round to, defaults to 2 * @param string $seperator string seperator to place between in the formatted output between the numerical size and the units, defaults to one space * @return array array of filesize information along with units and a per-formatted string output */ public function getFilesize($file, $units = "kb", $rounding = 2, $seperator = " ") { if (!$fileSize) { $returnSize = "err"; } else { switch ($lcUnits) { case "bits": $returnSize = $fileSize * 8; break; default: case "b": $returnSize = $fileSize; $units = "b"; break; case "kb": $returnSize = $fileSize / 1024; break; case "mb": $returnSize = $fileSize / 1048576; break; case "gb": $returnSize = $fileSize / 1073741824; break; case "tb": $returnSize = $fileSize / 1099511627776; break; } } } else { $returnSize = "FnF"; } "rawBytes" => $fileSize, "convertedSize" => $returnSize, "convertedSize_formatted" => $returnSize_formatted, "units" => $units, "formatted" => $returnSize_formatted . $seperator . $units ); return $return; }
You need to login to post a comment.
