File size function
This example shows how to format a file and present its size better
<?php
function format_size ($file)
{
//find the filesize of the file
$get_file_size = filesize($file);
//if the filesize has between 7 an 9 characters then its in Mb
if (strlen($get_file_size) <= 9 && strlen($get_file_size) >= 7)
{
$get_file_size = number_format($get_file_size / 1048576,1);
return “$get_file_size MB”;
}
//10 characters or over means Gb
elseif (strlen($get_file_size) >= 10)
{
$get_file_size = number_format($get_file_size / 1073741824,1);
return “$get_file_size GB”;
}
//anything else is kilobytes
else
{
$get_file_size = number_format($get_file_size / 1024,1);
return “$get_file_size KB”;
}
}
?>
Usage
<?php
echo format_size(”index.htm”);
echo “<br>”;
echo format_size(”monthimages.zip”);
echo “<br>”;
echo format_size(”sampledb.mdb”);
?>
Related posts:
- Check the amount of characters in a string function <?php //check the amount of characters in a string function...
- Count Lines in a Text File This example counts the amount of lines in a text...
- File list of a folder <?php //folder to check $folder = “C:/xampp/htdocs/xampp/wordpress/wp-includes”; $handle = opendir($folder);...
- File Details File Details using the RecursiveDirectoryIterator class <?php //change the $dir...
- Check a file exists Check a file exists using the file_exists function <?php $filename...
Related posts brought to you by Yet Another Related Posts Plugin.
Tags: filesize, number_format, strlen



















