Results 1 to 19 of 19

Thread: Convert FileSize

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Convert FileSize

    How can you convert file sizes to 1.41 KB, 12 MB, 128 bytes, etc?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    get the file size and write an equation to convert it.

    The filesize is always handed to you in bytes.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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)

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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;

    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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!)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    scoutt
    Guest
    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

  11. #11
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  12. #12
    scoutt
    Guest
    optional but

    You are strongly encouraged to always use curly braces even in situations where they are technically optional.

  13. #13

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    scoutt
    Guest
    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

  15. #15

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    it makes no difference...it's just plain stupid. White spaces don't matter in languages like C/C++, Perl, PHP...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  16. #16
    scoutt
    Guest
    I know, it is just for readability. I just thought I would mention it . no biggie

  17. #17
    scoutt
    Guest
    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.

  18. #18

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  19. #19
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width