/*
This function read the filesize from a selected file and convert the number of bytes into KB, MB etc. Just define the file and the path and the size is give by the function.
*/
// the $file without the path, put the path in the $path-variable (based on the doc. root)
function file_size($file, $path = "") {
global $DOCUMENT_ROOT;
$bytes = array("B", "KB", "MB", "GB", "TB", "PB");
$file_with_path = $DOCUMENT_ROOT."/".$path."/".$file;
// replace (possible) double slashes with a single one
$file_with_path = str_replace("//", "/", $file_with_path);
$size = filesize($file_with_path);
$i = 0;
while ($size >= 1024) { //divide the filesize (in bytes) with 1024 to get "bigger" bytes
$size = $size/1024;
$i++;
}
if ($i > 1) {
// you can change this number if you like (for more precision)
return round($size,1)." ".$bytes[$i];
} else {
return round($size,0)." ".$bytes[$i];
}
}
// example for using
echo file_size("example.txt", "myFolder");
?>