note 72700 added to function.filesize

From: Date: Tue, 30 Jan 2007 12:42:32 +0000
Subject: note 72700 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-122349@lists.php.net to get a copy of this message
There is a lot of functions for size of dir. But drive operations are slow, so if you want use those functions many times in one script, it's too slow. You can use this small function instead: <?php function dirsize($dir,$buf=2) { static $buffer; if(isset($buffer[$dir])) return $buffer[$dir]; if(is_file($dir)) return filesize($dir); if($dh=opendir($dir)) { $size=0; while(($file=readdir($dh))!==false) { if($file=='.' || $file=='..') continue; $size+=dirsize($dir.'/'.$file,$buf-1); } closedir($dh); if($buf>0) $buffer[$dir]=$size; return $size; } return false; } ?> What does it do? It save size in static array to cache result. You can choose deep value (e.q. if you want cache only first dir, use dirsize($dirname,1);). ---- Server IP: 83.64.128.68 Probable Submitter: 83.10.198.81 ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- https://master.php.net/note/edit/72700 Del: integrated -- https://master.php.net/note/delete/72700/integrated Del: useless -- https://master.php.net/note/delete/72700/useless Del: bad code -- https://master.php.net/note/delete/72700/bad+code Del: spam -- https://master.php.net/note/delete/72700/spam Del: non-english -- https://master.php.net/note/delete/72700/non-english Del: in docs -- https://master.php.net/note/delete/72700/in+docs Del: other reasons-- https://master.php.net/note/delete/72700 Reject -- https://master.php.net/note/reject/72700 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122349) next »