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:
- File list of a folder <?php //folder to check $folder = “C:/xampp/htdocs/xampp/wordpress/wp-includes”; $handle = opendir($folder);...
- FTP file upload example upload a single file to a FTP server // FTP...
- create a gzip file this example backs up the index.php as a gzip file...
- Line Counter example This is a simple line counter example which will count...
- temperature conversion function A simple temperature conversion function <?php function TempConv($temp,$corf) { ...
Related posts brought to you by Yet Another Related Posts Plugin.
Tags: filesize, number_format, strlen



















