File size

  • dario
  • Novice
  • Novice
  • dario
  • Posts: 31

Post 3+ Months Ago

this is part of my script for uploading
How to show size in kb, mb and bytes - I want to show size of uploaded file in all 3 sizes?


Code: [ Select ]
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "files/temp/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{

echo "Successful<BR/><BR/>";

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "<strong>File Name: </strong>".$HTTP_POST_FILES['ufile']['name']."<BR/><BR/>";
echo "<strong>File Size: </strong><br>".$HTTP_POST_FILES['ufile']['size']." bytes<BR/><BR/>";


echo "<strong>File Type: </strong>".$HTTP_POST_FILES['ufile']['type']."<BR/><BR/>";

}
else
{
echo "Error";
}
}

?>
  1. <?php
  2. //set where you want to store files
  3. //in this example we keep file in folder upload
  4. //$HTTP_POST_FILES['ufile']['name']; = upload file name
  5. //for example upload file name cartoon.gif . $path will be upload/cartoon.gif
  6. $path= "files/temp/".$HTTP_POST_FILES['ufile']['name'];
  7. if($ufile !=none)
  8. {
  9. if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
  10. {
  11. echo "Successful<BR/><BR/>";
  12. //$HTTP_POST_FILES['ufile']['name'] = file name
  13. //$HTTP_POST_FILES['ufile']['size'] = file size
  14. //$HTTP_POST_FILES['ufile']['type'] = type of file
  15. echo "<strong>File Name: </strong>".$HTTP_POST_FILES['ufile']['name']."<BR/><BR/>";
  16. echo "<strong>File Size: </strong><br>".$HTTP_POST_FILES['ufile']['size']." bytes<BR/><BR/>";
  17. echo "<strong>File Type: </strong>".$HTTP_POST_FILES['ufile']['type']."<BR/><BR/>";
  18. }
  19. else
  20. {
  21. echo "Error";
  22. }
  23. }
  24. ?>
  • nexuslite
  • Newbie
  • Newbie
  • nexuslite
  • Posts: 10
  • Loc: Keizer, OR

Post 3+ Months Ago

divide provided file size by 1024 for kB and divide it by 1048576 for MB also using ceil() will clear any decimal places.

Code: [ Select ]
echo $HTTP_POST_FILES['ufile']['size'].' Bytes';
echo ceil($HTTP_POST_FILES['ufile']['size']/1024).' kBytes';
echo ceil($HTTP_POST_FILES['ufile']['size']/1048576).' MBytes';
  1. echo $HTTP_POST_FILES['ufile']['size'].' Bytes';
  2. echo ceil($HTTP_POST_FILES['ufile']['size']/1024).' kBytes';
  3. echo ceil($HTTP_POST_FILES['ufile']['size']/1048576).' MBytes';
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I use the following class to tally up and display file sizes. There's really no answering the question any better than nexuslite did, but perhaps this class will come in handy. If for nothing else, a reference to how many bytes it takes to move up to the next human-readable unit. :D

PHP Code: [ Select ]
<?php
class large_filesize
{
   public $size, $precision;
   protected static $suffixes = array(
      'Y' => '1208925819614629174706176',
      'Z' => '1180591620717411303424',
      'E' => '1152921504606846976',
      'P' => '1125899906842624',
      'T' => '1099511627776',
      'G' => '1073741824',
      'M' => '1048576',
      'K' => '1024'
   );
   
   public function add()
   {
      foreach(func_get_args() as $num)
      {
            switch(true)
            {
                  case $num instanceof large_filesize:// Let the __toString methods of other objects handle themselves
                        $this->size = bcadd($this->size, $num->size);
                  break;
           
                  default:
                        $this->size = bcadd($this->size, $num);
                  break;
            }
      }
      return $this;
   }
   
   public function sub()
   {
      foreach(func_get_args() as $num)
      {
            switch(true)
            {
                  case $num instanceof large_filesize:// Let the __toString methods of other objects handle themselves
                        $this->size = bcsub($this->size, $num->size);
                  break;
           
                  default:
                        $this->size = bcsub($this->size, $num);
                  break;
            }
      }
   }
   
   public function __construct($size = '0', $precision = 2)
   {
      $this->precision = (int)$precision;
      $this->size = (string)$size;
      return $this;
   }
 
 
   public function __toString()
   {
      foreach(self::$suffixes as $suffix => $divisor)
      {
            if(bccomp($this->size, $divisor) > -1)
            {
                  return bcdiv($this->size, $divisor, (int)$this->precision) . $suffix;
            }
      }
      return $this->size . 'b';
   }
}
?>
  1. <?php
  2. class large_filesize
  3. {
  4.    public $size, $precision;
  5.    protected static $suffixes = array(
  6.       'Y' => '1208925819614629174706176',
  7.       'Z' => '1180591620717411303424',
  8.       'E' => '1152921504606846976',
  9.       'P' => '1125899906842624',
  10.       'T' => '1099511627776',
  11.       'G' => '1073741824',
  12.       'M' => '1048576',
  13.       'K' => '1024'
  14.    );
  15.    
  16.    public function add()
  17.    {
  18.       foreach(func_get_args() as $num)
  19.       {
  20.             switch(true)
  21.             {
  22.                   case $num instanceof large_filesize:// Let the __toString methods of other objects handle themselves
  23.                         $this->size = bcadd($this->size, $num->size);
  24.                   break;
  25.            
  26.                   default:
  27.                         $this->size = bcadd($this->size, $num);
  28.                   break;
  29.             }
  30.       }
  31.       return $this;
  32.    }
  33.    
  34.    public function sub()
  35.    {
  36.       foreach(func_get_args() as $num)
  37.       {
  38.             switch(true)
  39.             {
  40.                   case $num instanceof large_filesize:// Let the __toString methods of other objects handle themselves
  41.                         $this->size = bcsub($this->size, $num->size);
  42.                   break;
  43.            
  44.                   default:
  45.                         $this->size = bcsub($this->size, $num);
  46.                   break;
  47.             }
  48.       }
  49.    }
  50.    
  51.    public function __construct($size = '0', $precision = 2)
  52.    {
  53.       $this->precision = (int)$precision;
  54.       $this->size = (string)$size;
  55.       return $this;
  56.    }
  57.  
  58.  
  59.    public function __toString()
  60.    {
  61.       foreach(self::$suffixes as $suffix => $divisor)
  62.       {
  63.             if(bccomp($this->size, $divisor) > -1)
  64.             {
  65.                   return bcdiv($this->size, $divisor, (int)$this->precision) . $suffix;
  66.             }
  67.       }
  68.       return $this->size . 'b';
  69.    }
  70. }
  71. ?>

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 89 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.