PHP filesize

14.Jul.2010

In order to check size of a file, use PHP filesize function as shown in following snippet. This funtion will return you a file size in easy-to-read format.

1
2
3
4
5
6
7
8
9
function get_file_size($file) {
	$size = filesize($file);
	$units = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
 
	foreach($units as $pow => $unit) {
		if($size / pow(1024, $pow) < 1024)
			return number_format($size / pow(1024, $pow)) . ' ' . $unit;
	}
}