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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.