linein designs

03 Jun, 2008

Quick PHP Function to Get File Sizes in KB, MB & GB

Posted by: john In: php|programing

Here’s the follow-up article on how to easily get file sizes nicely formated. Below is a quick function that will display the file size in KB, MB, or GB all easily accessed through a function.

function formatbytes($file, $type)
{
	switch($type){
		case "KB":
			$filesize = filesize($file) * .0009765625; // bytes to KB
		break;
		case "MB":
			$filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB
		break;
		case "GB":
			$filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB
		break;
	}
	if($filesize <= 0){
		return $filesize = 'unknown file size';}
	else{return round($filesize, 2).' '.$type;}
}
// USAGE
echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");
// would display the file size in MB

Just remember the file location has to be the absolute location on the server. It can not be relative ('/images/....') Please let me know if you have any questions about any of this.

Similar Posts:

Tags:

11 Comments »

Comment by علی
 
 
Comment by Matt

I’ve tried using the above function, but when the code gets to the bit that displays the filesize it stops running and nothing below that point runs. I suspect it could be to do with the path to my image, but I’ve tried lots of variations without success. Also, I don’t understand the $_SERVER[DOCUMENT_ROOT] – I’ve tried with and without this. Any suggestions?

 
Comment by Matt

I think I’ve narrowed it down to something in the main function block of code, but I don’t know what? Is there anything in there that is only compatible with PHP5 as my server is PHP4 at the moment?

Comment by john

This should work with PHP4 & 5

 
 
Comment by Manolis

Hi,
Thanks for the script.
May i ask you something?
How can we modify that,so that in case that the file is under 1mb to be shown as number in KB and if it is more than 1mb then to be shown in MB?
Thanks. :)

Comment by john

That’s a good idea. I’ll post an updated version shortly that will have that feature.

 
 
Comment by wonko

Wonderful! Exactly what i needed today :-)

 
Comment by vinikey
 
Comment by John P

This is more versatile, in my opinion: http://aidanlister.com/2004/04/human-readable-file-sizes/

No need to specify a type, it does it automatically and gives other options…

 
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.