|
|
 |
filesize (PHP 3, PHP 4 ) filesize -- Gets file size Descriptionint filesize ( string filename)
Returns the size of the file in bytes, or FALSE in case of an error.
The results of this function are cached. See
clearstatcache() for more details.
This function will not work on remote files; the file to
be examined must be accessible via the server's filesystem.
User Contributed Notes filesize |
 |
mufasa at firetiger dot net
29-Jan-2002 09:25 |
|
NOTE: The filesize() function will report the usuall PHP run-time error _in
addition to_ returning false if it can't find the file. I assume any error
with this function will report an error too, but I've only tested for the
file not found condition.
If you're expecting or allowing the script
to go on after any such error, just add the @ symbol before it to ignore
the error. As always with the @ symbol though, still check any variables
that were expected to have a value in them from this function before you
use them.
|
|
x at x dot com
20-Apr-2002 03:30 |
|
If you want to check the size in order to limit the filesize, this should
work.
---------------------------------
function
remotefilesize($file) { // SETTINGS $max_file_size =
"1000" // in bytes
// Open the file, but only read
maximum size $fp = fopen($file, "r");
$first_file_size_read = fread($fp, $max_file_size);
fclose($fp);
// Open the file plus one byte... // if
there is an extra byte, then it is too big, $fp =
fopen("$image", "r"); $second_file_size_read =
fread($fp, $max_file_size + 1); fclose($fp);
// Check if
they're the same if ($first_file_size_read ==
$second_file_size_read) { $file_size_ok ==
"yes";
} }
--------------------------------
What you do with
$file_size_ok is up to you after this.
This should work, but it's a
dirty hack.
Good luck!
|
|
webmaster at tapetky dot cz
27-May-2002 12:45 |
|
How many place is free? How many dirs and files is on my
web?
-------------------------------------------- $quota="100";
// place for your web in MB
function
obsah($adr,&$total,&$dir,&$size){
$dp=OpenDir($adr);
do{ $itm=ReadDir($dp); if
(Is_Dir("$adr/$itm")&&($itm!=".")&&($itm!="..")&&($itm!="")){
obsah("$adr/$itm",$total,$dir,$size); $dir++;
} elseif
(($itm!=".")&&($itm!="..")&&($itm!="")){
$size=$size+FileSize("$adr/$itm"); $total++;
} } while ($itm!=false);
CloseDir($dp); }
obsah(".",$total,$dir,$size); $freeA=BcDIV($size,1024*1024,2); $freeB=$quota-$freeA;
echo
"<CENTER><FONT FACE=arial SIZE=2> Engaged place:
<B>$freeA</B> MB Free place: <B>$freeB</B>
MB <B>$total</B> files in <B>$dir</B>
dirs </FONT></CENTER>"; --------------------------------------------
|
|
sponger
10-Jun-2002 06:28 |
|
This may come in handy for someone. Returns the size of the passed file in
the appropriate measurement format.
function my_filesize($file)
{ // First check if the file exists.
if(!is_file("./".$file)) exit("File does not
exist!"); // Setup some common file size measurements.
$kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; //
Terabyte // Get the file size in bytes. $size =
filesize($file); /* If it's less than a kb we just return the size,
otherwise we keep going until the size is in the appropriate
measurement range. */ if($size < $kb) { return
$size." B"; } else if($size < $mb) {
return round($size/$kb,2)." KB"; } else if($size
< $gb) { return round($size/$mb,2)." MB";
} else if($size < $tb) { return
round($size/$gb,2)." GB"; } else {
return round($size/$tb,2)." TB"; } }
|
|
andudi at gmx dot ch
11-Jun-2002 11:45 |
|
To find the total size of a file/directory you have to differ
two situations: (on Linux/Unix based systems only!?)
you are
interested: 1) in the total size of the files in the dir/subdirs 2)
what place on the disk your dir/subdirs/files uses
- 1) and 2)
normaly differs, depending on the size of the inodes - mostly 2) is
greater than 1) (in the order of any kB) - filesize($file) gives 1) -
"du -ab $file" gives 2)
so you have to choose your
situation!
on my server I have no rights to use "exec du"
in the case of 2), so I use: $s = stat($file); $size =
$s[11]*$s[12]/8); whitch is counting the inodes [12] times the size of
them in Bits [11]
hopes this helps to count the used disk place in a
right way... :-)
Andreas Dick
|
|
andreaSPAMLESS@SPAMLESScriticalbitDOTcom
04-Jul-2002 11:29 |
|
I've just implemented an easy&extensible function for calculating file
size in an human readable format. It does'n check any file, it just
formats any given number.
Hope it helps anyone ;)
/** *
Format file size in a human-readable way * such as "xx
Megabytes" or "xx Mo" * * @author Andrea
Paleni <andreaSPAMLESS_AT_SPAMLESScriticalbit.com> * @version
1.0 * @param int bytes is the size * @param bool
base10 enable base 10 representation, otherwise *
default base 2 is used * @param int round number of
fractional digits * @param array labels strings associated to each
2^10 or * 10^3(base10==true) multiple of base
units */ function hbytes ($bytes, $base10=false, $round=0,
$labels=array('bytes', 'Kb', 'Mb', 'Gb')) { if
(($bytes <= 0) || (! is_array($labels)) ||
(count($labels) <= 0)) return null; $step =
$base10 ? 3 : 10 ; $base = $base10 ? 10 : 2; $log =
(int)(log10($bytes)/log10($base)); krsort($labels);
foreach ($labels as $p=>$lab) { $pow = $p *
$step; if ($log < $pow) continue; $text =
round($bytes/pow($base,$pow),$round) . $lab; break; }
return $text; }
Notice that you can pass any number of
labels: the function automatically uses the biggest one
available. E.g. If you pass only "bytes" , then every file
will have it's size printed in bytes. (almost useless...)
If you
add "Kb" , then any filesize bigger than 1Kb will use Kb
instead.
Works similarly for any bigger unit until Tera or even Peta
bytes :)
Maybe there's some bug I didn't notice (Murphy docet) , so
please warn me if it occurs.
|
|
|
31-Jul-2002 02:05 |
|
In sponger's my_filesize he multiplies these every single time my_filesize
is called I think it'd probably be faster to hard code the digits like
so.
$kb=1024; $mb=1048576; $gb=1073741824; $tb=1099511627776;
|
|
cnww at a host called k-pcltee dot com
24-Sep-2002 12:22 |
|
A nice short human readable function: function human_readable(
$number, $base=1024, $suffixes=array( " B", "
KB", " MB", " GB", " TB", "
PB", " EB" ) ) { $usesuf = 0; $n =
(float) $number; //Appears to be necessary to avoid rounding while(
$n >= $base ) { $n /= (float) $base; $usesuf++;
} $places = 2 - floor( log10( $n ) ); $places = max( $places,
0 ); $retval = number_format( $n, $places, ".",
"" ) . $suffixes[$usesuf]; return $retval; }
|
|
PHPMatrix
13-Oct-2002 12:25 |
|
I found this nice little human readable file size code. It seems simpler
and easier to understand. I'm a PHP newbee so that's why I say its easy to
understand. I don't get the above codes, lol. Hope it helps some other
newbe, lol.
<?php $my_file =
"/path/to/my/file"; $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 "File $my_file is $show_filesize
"; ?>
Ryan
|
|
 |
| |