PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

Find size of a file or image

May5

Find size of a file stored physically on your web folder and its address in a database.

You can find the filesize using an inbuild PHP function filesize(). And display the filesize in human readable format use again PHP function number_format

Example

Assume that all the files are stored in a folder called download

$my_file=download/abc.doc;

$file_size = filesize($my_file);

if ($file_size >= 1073741824) { $show_filesize = number_format(($file_size / 1073741824),2) . ” GB”; }
elseif ($file_size >= 1048576) { $show_filesize = number_format(($file_size / 1048576),2) . ” MB”; }
elseif ($file_size >= 1024) { $show_filesize = number_format(($file_size / 1024),2) . ” KB”; }
elseif ($file_size >= 0) { $show_filesize = $file_size . ” bytes”; }
else { $show_filesize = “0 bytes”; }
echo $show_filesize;

abc.doc is the filename but where did we get it? answer from the database. How? like this->

Assume the database has already been connected. Now open the table retrieve the file name. Assume again a table called file exists with what ever fields.

$res=mysql_query(”select * from file”);
$row=mysql_fetch_array($res);
$my_file=$row["filename"];

posted under PHP

Email will not be published

Website example

Your Comment:

 

12,958 spam comments
blocked by
Akismet