|
-
Jun 9th, 2002, 06:00 PM
#1
Thread Starter
Stuck in the 80s
Convert FileSize
How can you convert file sizes to 1.41 KB, 12 MB, 128 bytes, etc?
-
Jun 9th, 2002, 06:40 PM
#2
Fanatic Member
get the file size and write an equation to convert it.
The filesize is always handed to you in bytes.
-
Jun 9th, 2002, 07:07 PM
#3
Thread Starter
Stuck in the 80s
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.
-
Jun 9th, 2002, 07:28 PM
#4
PowerPoster
If i'm understanding you, you want something like this, right?
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>";
}
}
?>
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)
-
Jun 9th, 2002, 07:35 PM
#5
Thread Starter
Stuck in the 80s
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;
}
-
Jun 9th, 2002, 09:03 PM
#6
Fanatic Member
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.
-
Jun 9th, 2002, 09:12 PM
#7
Thread Starter
Stuck in the 80s
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.
-
Jun 9th, 2002, 09:19 PM
#8
Fanatic Member
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
-
Jun 9th, 2002, 09:28 PM
#9
Thread Starter
Stuck in the 80s
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!)
-
Jun 10th, 2002, 10:57 AM
#10
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.
that is wrong. for the PEAR standard you should have them in there.
http://pear.php.net/manual/en/standards.control.php
-
Jun 10th, 2002, 10:59 AM
#11
Fanatic Member
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.
-
Jun 10th, 2002, 11:06 AM
#12
optional but
You are strongly encouraged to always use curly braces even in situations where they are technically optional.
-
Jun 10th, 2002, 11:07 AM
#13
Thread Starter
Stuck in the 80s
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.
Give me a break...
-
Jun 10th, 2002, 11:10 AM
#14
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
-
Jun 10th, 2002, 11:12 AM
#15
Thread Starter
Stuck in the 80s
it makes no difference...it's just plain stupid. White spaces don't matter in languages like C/C++, Perl, PHP...
-
Jun 10th, 2002, 11:13 AM
#16
I know, it is just for readability. I just thought I would mention it . no biggie
-
Jun 10th, 2002, 11:26 AM
#17
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

but hey a standard is a standard. folloow it if you want. I am not trying to twist your arm or anyting.
-
Jun 10th, 2002, 11:28 AM
#18
Thread Starter
Stuck in the 80s
Originally posted by scoutt
it is when you have a script that is 3000 lines
No, it's not...
Originally posted by scoutt
but then you wouldn't know that since you can't code anything over what a script kiddie does
ouch...
-
Jun 10th, 2002, 11:59 AM
#19
Fanatic Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|