Tuesday, March 31, 2015

Smart displaying of file size - PHP Snippet

Smart displaying of file size - PHP Snippet


This php function will smartly display the file size, instead of displaying the size in bytes, why not display GB, MB, KB or bytes as well.


function file_size($url){ 
$size = filesize($url);
if($size >= 1073741824){
$fileSize = round($size/1024/1024/1024,1) . 'GB';
}elseif($size >= 1048576){
$fileSize = round($size/1024/1024,1) . 'MB';
}elseif($size >= 1024){
$fileSize = round($size/1024,1) . 'KB';
}else{
$fileSize = $size . ' bytes';
}
return $fileSize;
}
echo file_size('/myfile/image.jpg');
Disqus Comments