Php question for gurus: Displaying file size in MB not in bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pics Traffic
    Confirmed User
    • Jun 2004
    • 3055

    #1

    Php question for gurus: Displaying file size in MB not in bytes

    How do you do it?

    echo filesize("0001.wmv");

    is doing to show bytes. How do you format it to show in MB's ?
    My Network Is Waiting For Your Fuckin' Trade!
  • Voodoo
    ♥ ♦ ♣ ♠
    • Sep 2002
    • 10600

    #2
    mb=bytes\1024\1024
    Last edited by Voodoo; 02-04-2009, 04:26 PM.

    "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

    Comment

    • Juicy D. Links
      So Fucking Banned
      • Apr 2001
      • 122992

      #3
      Code:
        
      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;}   
      }

      Code:
      // USAGE   
      echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");   
      // would display the file size in MB
      Last edited by Juicy D. Links; 02-04-2009, 04:28 PM.

      Comment

      • Pics Traffic
        Confirmed User
        • Jun 2004
        • 3055

        #4
        Originally posted by Juicy D. Links
        Code:
          
        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;}   
        }

        Code:
        // USAGE   
        echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");   
        // would display the file size in MB

        you're the man Juicy. Now do the eyebrow for me again like you did in Atlanta at the bar. I need to show you that pix Thanks Bro.
        My Network Is Waiting For Your Fuckin' Trade!

        Comment

        • Juicy D. Links
          So Fucking Banned
          • Apr 2001
          • 122992

          #5
          Originally posted by MOCKBA
          you're the man Juicy. Now do the eyebrow for me again like you did in Atlanta at the bar. I need to show you that pix Thanks Bro.


          hehehe

          Comment

          • Bro Media - BANNED FOR LIFE
            MOBILE PORN: IMOBILEPORN
            • Jan 2004
            • 16502

            #6
            Originally posted by Juicy D. Links
            Code:
              
            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;}   
            }

            Code:
            // USAGE   
            echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");   
            // would display the file size in MB
            Juicy knows PHP? LIES!!

            Good job, I was about to post something similar for him.

            Comment

            • Ozarkz
              So Fucking Banned
              • Jan 2009
              • 2377

              #7
              Originally posted by Retox Josh
              Juicy knows PHP? LIES!!

              Good job, I was about to post something similar for him.

              No you weren't.

              Comment

              • Juicy D. Links
                So Fucking Banned
                • Apr 2001
                • 122992

                #8
                the code is from google btw

                Comment

                • Bro Media - BANNED FOR LIFE
                  MOBILE PORN: IMOBILEPORN
                  • Jan 2004
                  • 16502

                  #9
                  Originally posted by Ozarkz
                  No you weren't.
                  Fuck off fake nick.

                  Comment

                  • Azathoth
                    Confirmed User
                    • Nov 2002
                    • 217

                    #10
                    Originally posted by Juicy D. Links
                    Code:
                      
                    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;}   
                    }

                    Code:
                    // USAGE   
                    echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");   
                    // would display the file size in MB
                    On the reutrn you could also do
                    return(number_format($filesize, 2, '.', ',').' '.$type)

                    This would show something like 1,321.26 KB etc.

                    - Az

                    Comment

                    • ExLust
                      Confirmed User
                      • Aug 2008
                      • 3223

                      #11
                      Originally posted by Juicy D. Links
                      Code:
                        
                      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;}   
                      }

                      Code:
                      // USAGE   
                      echo formatbytes("$_SERVER[DOCUMENT_ROOT]/images/large_picture.jpg", "MB");   
                      // would display the file size in MB
                      Thanks. I save this.

                      BE A PARTNER

                      Comment

                      • edgeprod
                        Permanently Gone
                        • Mar 2004
                        • 10019

                        #12
                        Originally posted by Retox Josh
                        Juicy knows PHP? LIES!!

                        Good job, I was about to post something similar for him.
                        It would have been hilarious if you saw what he posted before the edit. I did a double-take and was like "wait, that's a built-in function now?!" hehe.

                        But props for finding it and posting it, Juicy.

                        Comment

                        • Juicy D. Links
                          So Fucking Banned
                          • Apr 2001
                          • 122992

                          #13
                          Originally posted by edgeprod
                          It would have been hilarious if you saw what he posted before the edit. I did a double-take and was like "wait, that's a built-in function now?!" hehe.

                          But props for finding it and posting it, Juicy.
                          only have basic code exp from years ago just basics n shit lolll yeah i had to look at the code before i realized it was missing something loll

                          Comment

                          • Bro Media - BANNED FOR LIFE
                            MOBILE PORN: IMOBILEPORN
                            • Jan 2004
                            • 16502

                            #14
                            Originally posted by edgeprod
                            It would have been hilarious if you saw what he posted before the edit. I did a double-take and was like "wait, that's a built-in function now?!" hehe.

                            But props for finding it and posting it, Juicy.
                            Originally posted by Juicy D. Links
                            only have basic code exp from years ago just basics n shit lolll yeah i had to look at the code before i realized it was missing something loll
                            Lmfao, I take it he just posted the usage bit? Nice one Juicy!

                            Comment

                            • nation-x
                              Confirmed User
                              • Mar 2004
                              • 5370

                              #15
                              Juicy has giant code balls

                              Comment

                              Working...