Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => PHP => Topic started by: Hellraiser on August 29, 2007, 10:32:24 AM



Title: Find size of a file or image in PHP
Post by: Hellraiser on August 29, 2007, 10:32:24 AM
Find size of a file or image in PHP

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

Code:
$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.

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