PHP Snippets
filesize_tag
Information
(PHP Version >= 4)Functions used in this snippet: floor, array_key_exists, implode, array_reverse, sizeof.
Description
str filesize_tag( int size[, bool full] )
Takes a filesize of size and turns it into its textual form. If the optional field full is set to true, the complete filesize will be returned.
Snippet
<?php
function filesize_tag($size,$full=false){
$sizes = array();
$tags = array(' B', ' KB', ' MB', ' GB', ' TB');
$i = 0;
$sizes[$i] = $size;
$i++;
while($size > 1024){
$size = floor($size / 1024);
$sizes[$i] = $size;
$i++;
}
foreach($sizes as $key=>$size){
$subtract = array_key_exists(($key + 1), $sizes) ? $sizes[$key + 1] * 1024 : 0;
$sizes[$key] = $sizes[$key] - $subtract . $tags[$key];
}
return $full ? implode(', ',array_reverse($sizes)) : $sizes[sizeof($sizes) - 1];
}
?>
Example
filesize_tag Can be used in the following way:
<?php
echo filesize_tag(123456); // 120 KB
echo filesize_tag(123456789); // 117 MB
echo filesize_tag(12345,true); // 12 KB, 57 B
echo filesize_tag(filesize('somefile.txt')); // size of the file...
?>
0 Responses to filesize_tag