note 80357 added to function.filesize
| From: | maskoflight at centrum dot cz | Date: | Fri, 11 Jan 2008 18:33:05 +0000 |
| Subject: | note 80357 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135030@lists.php.net to get a copy of this message | ||
I found a mistake in note from "RobKohrPhp at remail dot robkohr dot com, 04-Feb-2006
09:42".
His simplified function isn't working correct:
<?php
function get_size($path)
{
if (!is_dir($path)) return filesize($path);
if ($handle = opendir($path)) {
$size = 0;
while (false !== ($file = readdir($handle))) {
if($file!='.' && $file!='..'){
$size += filesize($path.'/'.$file); // this line affects that size of file is
added two times
$size += get_size($path.'/'.$file);
}
}
closedir($handle);
return $size;
}
}
?>
marked line affects that size of file is added two times, because function get_size is called on the
next line, which adds a size correctly (if $file is dir, it will be add only with get_size calling,
if $file is a file, it will be add with both functions filesize and get_size)
Here is a correction:
<?php
function get_size($path)
{
if(!is_dir($path)) return filesize($path);
if ($handle = opendir($path)) {
$size = 0;
while (false !== ($file = readdir($handle))) {
if($file!='.' && $file!='..') {
// function filesize has been deleted
$size += get_size($path.'/'.$file);
}
}
closedir($handle);
return $size;
}
}
?>
----
Server IP: 147.32.160.17
Probable Submitter: 80.188.120.114
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/80357
Del: integrated -- https://master.php.net/note/delete/80357/integrated
Del: useless -- https://master.php.net/note/delete/80357/useless
Del: bad code -- https://master.php.net/note/delete/80357/bad+code
Del: spam -- https://master.php.net/note/delete/80357/spam
Del: non-english -- https://master.php.net/note/delete/80357/non-english
Del: in docs -- https://master.php.net/note/delete/80357/in+docs
Del: other reasons-- https://master.php.net/note/delete/80357
Reject -- https://master.php.net/note/reject/80357
Search -- https://master.php.net/manage/user-notes.php