How can you convert file sizes to 1.41 KB, 12 MB, 128 bytes, etc?
Printable View
How can you convert file sizes to 1.41 KB, 12 MB, 128 bytes, etc?
get the file size and write an equation to convert it.
The filesize is always handed to you in bytes.
I know...the equation is what I don't know. I know how to tell if it's bytes, kb, or mb...but how to display it as 1.4mb, or 12 KB, I don't know.
If i'm understanding you, you want something like this, right?
That's a quick and dirty example which lists all the picture files in a directory along with their size in an appropriate measure, e.g > 819kb will show as 0.8 MB (ala Windows Explorer)PHP Code:<?php
$dir = opendir('.'); //current dir, change to whatever
while ($file = readdir($dir)) {
if (eregi("gif$|png$|jpg$|bmp$|jpg$|jpeg$", $file)) {
// calculate size
$size_bytes = filesize($file);
if ($size_bytes > 858992640) {
// greater than 0.8 Gb, show in GB
$size = sprintf("%.2f", ((($size_bytes / 1024) / 1024) / 1024)).' GB';
} elseif ($size_bytes > 838860) {
// greater than 0.8 Mb, show in Mb
$size = sprintf("%.2f", (($size_bytes / 1024) /1024)).' MB';
} elseif ($size_bytes > 819) {
// greater than 0.8 kb, show in kb
$size = sprintf("%.2f", ($size_bytes / 1024)).' KB';
} else {
// really small, show in bytes
$size = sprintf("%.2f", $size_bytes).' Bytes';
}
print "Name: $file Size: $size<br><br>";
}
}
?>
Thanks! I changed it a bit:
PHP Code:$size = 224554;
echo getsize($size);
function getsize($fs) {
if ($fs > 858992640) {
$size = sprintf("%.2f", ((($fs / 1024) / 1024) / 1024)).' GB';
} elseif ($fs > 838860) {
$size = sprintf("%.2f", (($fs / 1024) /1024)).' MB';
} elseif ($fs > 819) {
$size = sprintf("%.2f", ($fs / 1024)).' KB';
} else {
$size = sprintf("%.2f", $fs).' Bytes';
}
return $size;
}
Hobo: if you want to make yours more effecient take out the { and } in the if statements as they are useless and not required for only one line.
I don't see how that makes it more effecient, and if you read my recent post in C++ Forums, you'd know that I know this. But I put them in there for readability, otherwise I confuse myself. I also think it's good coding practice.
Just my personal opinion, though.
The only reason its more effecient is when PHP, C, or C++ reads a bracket they expect more than one line of code to follow and so it makes more resources available to handle the next few lines.
I agree it helps with readability and it can be a great practice method. I guess you really only need to worry about that stuff when you get into a page that is about 1000 lines of code or so, and let me tell ya, I am in that situation just about everyday, so when I see that stuff it just strikes me as it should be done this way.
-Matt
Thanks for the information. :) If I'm ever in that many line situation, I'll take that into consideration. (I rhyme one fat tune, yo!)
that is wrong. for the PEAR standard you should have them in there.Quote:
Originally posted by cpradio
Hobo: if you want to make yours more effecient take out the { and } in the if statements as they are useless and not required for only one line.
http://pear.php.net/manual/en/standards.control.php
They said exactly what I said. They are optional, and only increase reading ability. They will not be required b/c in large lengths of code you save both size and effecientcy by removing the unneeded ones.
optional but
You are strongly encouraged to always use curly braces even in situations where they are technically optional.
Give me a break...:rolleyes:Quote:
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon.
actually is sounds stupid, but it makes the code more readability. I thought the same as you until I started coding it like that. it makes a big improvement.
but this is only optional, just like all the browser standards :D
it makes no difference...it's just plain stupid. White spaces don't matter in languages like C/C++, Perl, PHP...
I know, it is just for readability. I just thought I would mention it . no biggie
it is when you have a script that is 3000 lines, but then you wouldn't know that since you can't code anything over what a script kiddie does
:D
but hey a standard is a standard. folloow it if you want. I am not trying to twist your arm or anyting.
No, it's not...Quote:
Originally posted by scoutt
it is when you have a script that is 3000 lines
ouch...:rolleyes:Quote:
Originally posted by scoutt
but then you wouldn't know that since you can't code anything over what a script kiddie does
One thing about programming is someone standards and opinions can totally bug the "hell" out of another programmer.
I personally program my scripts in my own technique so I understand it and am used to it. I never change ways b/c then I will look at the code a week later and say "What is this!"
Personally I do not try to get others to change their coding, but rather point out ways where you can optimize the file size and the load you are about to put the server through. You can take my opinions seriously or just as a good note to sometime reference to or even ignore it for all I care.
Its just half the people I work with never new { } were optional along and that by removing excess whitespace you decrease your file size. They thought the white space didn't take any storage space (Go Figure!).
I agree readability is key to coding and everyone has their own style/technique and I think they should only change it if the script seems to be running a lot slower than what they are used to.
Scripts can become massive quickly, and the load you put on the server doesnt just harm you but the others who use the same server, your visitors, members, etc.
I will agree with Scoutt that the PEAR method is a good way to write your scripts, but I would say its better to do it that way when first learning. Once you get the hang of php and find some easy methods for debugging scripts readability really isnt as much as an issue anymore. I can open scripts I wrote 2 years ago that look like crappy coding and read through it with little effort. YOu could almost say there comes a time to where you do not even realize its code but rather its a native language.
-Matt