linein designs

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:

10 Comments »

Comment by Eric

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.

Comment by john

Glad it could help. Thanks for the correction – it’s been fixed.

 
 
 
Comment by Abeon

A very simple and effective solution, thanks for sharing it!

 
Comment by telo

thanks for the code. really usefull

 
Comment by Erick Levy

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;
}

 
Comment by peter

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 ...

 
Comment by john

thanks for letting me know – it’s been fixed

 
Comment by Pete west

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?

 
Comment by Pete west

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?)

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post


  • Seren: Thanks - google did not fail me in finding this snippet (after 2 hours of 'why isn't this working!' before I choose to look for answers. That select
  • zdenis: type your scp command with nohup at the beginning: >nohup scp... enter your password stop temporarily the command with CTRL+z put on b
  • Criz: php script >> How to get the number of hours and minutes of a two set of time like this sample. 1st set of time : 8:00 to 17:00 2nd set of


Please Support Our Friends

Banner
FreshBooks

About

My name is John Veldboom and have been in design and web programing for just over 8 years now. Started off as a hobby in high school but it has developed into a full time job now. I always tell people that if I wasn't doing this at work each day, I would be at home doing it for free. I love it!

Please visit the contact page to drop me a message.