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(($file / 1024), 2); // bytes to KB
$file_mb = round(($file / 1048576), 2); // bytes to MB
$file_gb = round(($file / 1073741824), 2); // bytes to GB
// PHP does funny thing for files larger than 2GB
Similar Posts:
- Fade In and Out Images from a Single Directory Using jQuery (with plugin)
- Find Time Between Two Dates in PHP
- Display Last.fm’s Recent Tracks on Web Site with PHP Function
- Quick PHP Function to Get File Sizes in KB, MB & GB
- Find Years and Months Between Two Dates in PHP

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!