25 Apr, 2007
PHP File Last Modified and Filesize (bytes to KB/MB)
Posted by: john In: php|programing
Here’s a quick snip of code to help you easily manage your files by finding out the time the file was created or modified and also the time of the file.
$file = 'image.jpg';
// FILE TIME
$filetime = filemtime($file); // displays in Seconds since the Unix Epoch
echo date("M j, Y", $filetime); // would display the file creation/modified date as Feb 3, 2007
// FILE SIZES
$filesize = filesize($file); // displays in bytes
$file_kb = round(($filesize / 1024), 2); // bytes to KB
$file_mb = round(($filesize / 1048576), 2); // bytes to MB
$file_gb = round(($filesize / 1073741824), 2); // bytes to GB
// PHP does funny thing for files larger than 2GB
Similar Posts:
- Find Time Between Two Dates in PHP
- Find Years and Months Between Two Dates in PHP
- Fade in and Out the Most Recent Pictures in a Directory with jQuery
- Display Last.fm’s Recent Tracks on Web Site with PHP Function
- Quick PHP Function to Get File Sizes in KB, MB & GB


March 1st, 2008 @ 9:34 pm
Exactly what I was looking for. Just change the comment for the second line to “// bytes to MB” instead of KB. It’s a little misleading.
March 2nd, 2008 @ 8:56 am
Glad it could help. Thanks for the correction – it’s been fixed.
October 20th, 2008 @ 8:19 am
ı like this blog . . .
April 5th, 2010 @ 9:01 am
A very simple and effective solution, thanks for sharing it!
February 26th, 2011 @ 11:33 pm
thanks for the code. really usefull
June 21st, 2011 @ 10:04 pm
How about using a function to return the filesize in the desired units or automatic mode:
function file_size($file,$unit='auto') {
$retval = false;
if (file($file)) {
$filesize = filesize($file);
if ($unit == 'GB' || $unit == 'auto' && $filesize >= pow(1024,3)) {
$retval = round(($filesize / pow(1024,3)),2) . 'GB';
} elseif ($unit = 'MB' || $unit == 'auto' && $filesize >= pow(1024,2)) {
$retval = round(($filesize / pow(1024,2)),2) . 'MB';
} elseif ($unit = 'KB' || $unit == 'auto' && $filesize >= 1024) {
$retval = round(($filesize / 1024),2) . 'KB';
} else {
$retval = $filesize . 'Bytes';
}
}
return $retval;
}
June 26th, 2011 @ 4:44 pm
Actually there is a typo in the code. In the round function the variable should be $filesize and not $file.
So the correct code should be:
$file_kb = round(($filesize / 1024), 2); // bytes to KB ...
June 28th, 2011 @ 1:28 pm
thanks for letting me know – it’s been fixed
October 22nd, 2011 @ 2:25 am
This is great, exactly what I have been looking for.
With Erick Levy’s code, I’m quite new with PHP, do I have to make a seperate functions.php file with that code in it? How does it exactly work?
October 22nd, 2011 @ 7:48 am
I actually found another code as well, this one autodetects which file size to use (KB/MB/GB)
function calc($size) {
$kb=1024;
$mb=1048576;
$gb=1073741824;
$tb=1099511627776;
if(!$size)
return ’0 B’;
elseif($size<$kb)
return $size.' B';
elseif($size<$mb)
return round($size/$kb, 2).' KB';
elseif($size<$gb)
return round($size/$mb, 2).' MB';
elseif($size<$tb)
return round($size/$gb, 2).' GB';
else
return round($size/$tb, 2).' TB';
}
All I need to know is how to make it work (how do I make that code display a files size?)