note 80357 deleted from function.filesize by aidan

From: Date: Sat, 02 May 2009 11:16:28 +0000
Subject: note 80357 deleted from function.filesize by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154114@lists.php.net to get a copy of this message
Note Submitter: maskoflight at centrum dot cz ---- 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; } } ?>

« previous php.notes (#154114) next »