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:

10 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



Please Support Our Friends

Send emails with ease on this SMTP server by JangoSmtp.com.

Banner

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.