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

UTF8 string length counter...

The best way to determine the character count of a UTF8 string. function strlen_utf8( $str ) { return mb_strlen( $str, 'UTF-8' ); }   //or... function strlen_utf8( $str ) { $c = 0;...

URL Checker...

This function check if an url is valid or not. function is_valid_url($url) { return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url); }...

Get all subsets of array...

If you have a list of items (for example)......

Parse a string into separate words...

Parse a string into separate words (array output). function parser($str, $char = ' ') { $str = trim($str); $str = $str . $char; $len = strlen($str); $words = array(); $w = '...