Human readable file size

This function return formatting of file sizes in a human readable format.

function formatFileSize( $size )
{
   $file_size = $size;
   $i = 0;
 
   $name = array( 'byte', 'kB', 'MB', 'GB' );
 
   while ( $file_size > 1000 ) {
      $i++;
      $file_size = $file_size / 1024;
   }
 
   return round( $file_size, 2 ) . " " . $name[$i];
 
}
 
echo formatFileSize( 1024 * 1024 ); //out = 1 MB

Tags

No tag here.

Recommended pages

Number Format...

Format a number with grouped thousands......

alphaID - Translates a number to a short alhanumeric version...

Translates a number to a short alhanumeric version. e.g.: 9007199254740989 --> PpQXn7COf In most cases this is better than totally random ID generators because this can easily avoid duplicate ID's. Fo...

Validate phone number...

Chek if a phone numbers is valid, according to its syntax (should be: "+390523599314"). True if it's valid, False otherwise function is_valid_phone( $phone ) { if ( $phone[0] != "+" ) { ...

Download a file from web to local...

Use this function to download a file from web to local machine. function WgetFile( $URL, $dir ) { $nomefile = $dir . "/" . basename( $URL ); if ( copy( $URL, $nomefile ) ) { retu...