Mega Code Archive

 
Categories / Php / Code Snippets
 

File size function

<?php function file_size($file) { //store the actual size of the file $size = filesize($file); if($size >= 1073741824) { $size = round($size / 1073741824) . " gigabytes "; } elseif($size >= 1048756) { $size = round($size / 1048576) . " megabytes "; } elseif($size >= 1024) { $size = round($size / 1024) . " kilobytes "; } else { $size = $size . " bytes "; } return $size; } ?> Now lets test this function out with the following . <?php echo "index page is " . file_size("index.htm"); echo "<br>"; echo "this page is " . file_size("filesize.php"); ?>