6 August 2013

This snippet shows you how to convert file size in bytes to nice (human-readable) format in PHP. If the size is less than 1 MB, show the size in KB, if it's between 1 MB - 1 GB show it in MB, etc.

Source code viewer
  1. $size = filesize($file_dir);
  2. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  3. $power = $size > 0 ? floor(log($size, 1024)) : 0;
  4. echo number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
Programming Language: PHP