Jump to content

about folder size


lakkilakki

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/120668-about-folder-size/#findComment-621809
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/120668-about-folder-size/#findComment-621810
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.