lakkilakki Posted August 21, 2008 Share Posted August 21, 2008 Hi all, I am new to Php.How to find folder size by using PHP.. Plzzzzzzz help me.. regards, Lakki Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 21, 2008 Share Posted August 21, 2008 Okay. Had some spare time, here is a small script that counts the number of files and how big the folder is. <?php $directory = "./dir/"; //the directory, needs trailing slash $tmp = opendir($directory); $totalSize = 0; $totalFiles = 0; while(false !== ($file = readdir($tmp))){ if($file != "." && $file != ".."){ $totalSize += filesize($directory.$file); $totalFiles += 1; } } $size = array("b" => $totalSize, "kb" => ($totalSize / 1024), "mb" => (($totalSize / 1024) / 1024), "gb" => ((($totalSize / 1024) / 1024) /1024)); echo $directory . " has " . $totalFiles ." files with a total size of " . round($size['mb'],2) . "mb"; ?> It stores the size in array with the different forms. Bytes, Kilobytes, Megabytes and Gigabytes. Quote Link to comment Share on other sites More sharing options...
stuffradio Posted August 21, 2008 Share Posted August 21, 2008 Not sure if this works: How to get folder size? Here is an answer. =========================================== function foldersize($path) { $total_size = 0; $files = scandir($path); foreach($files as $t) { if (is_dir($t)) { // In case of folder if ($t<>"." && $t<>"..") { // Exclude self and parent folder $size = foldersize($path . "/" . $t); // print("Dir - $path/$t = $size<br>\n"); $total_size += $size; } } else { // In case of file $size = filesize($path . "/" . $t); // print("File - $path/$t = $size<br>\n"); $total_size += $size; } } return $total_size; } Taken from php.net: http://ca3.php.net/manual/en/function.filesize.php#77900 http://ca3.php.net/filesize Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.