Get File size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dada1
    New Member
    • Jul 2008
    • 15

    Get File size

    THe script I want to modify shows file size like this
    Code:
    $size = fsize($target_path);
    If file is more then 1024kb it shows size in MB, if it is less then 1024kb it shows size in KB. Is it possible with IF-ELSE function to show size only in kb?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    That is a custom function, not a PHP function. The PHP function is: filesize() which returns the value in bytes. Simply divide that by 1024 to get kilobytes. You will also need to add your own units.

    If you want to use a better custom function, have a look here where a second parameter can be put in to say what unit you wanted the size in.

    Comment

    • dada1
      New Member
      • Jul 2008
      • 15

      #3
      Thanks, is this it?
      Code:
      function fsize($file) {
              $a = array("B", "KB", "MB", "GB", "TB", "PB");
              $pos = 0;
              $size = filesize($file);
              while ($size >= 1024) {
                      $size /= 1024;
                      $pos++;
              }
      
              return round($size,2)." ".$a[$pos];
      }
      How to modify that

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        This should give you everything in kB:
        Code:
        function fsize($file) {
                $size = filesize($file);
                $kbsize = $size / 1024;
                return round($kbsize,2)." kB"; 
        }

        Comment

        Working...