1

I am wanting to calculate the weight of a directory in php, then display the data as per the example below.

Example:

Storage
50 GB (14.12%) of 353 GB used

I have the following function, with which I show in a list the folders that are inside the root.

<?php

    $dir = ('D:\data');
    echo "Size : " Fsize($dir);
    function Fsize($dir)
        {
            if (is_dir($dir))
                {
                    if ($gd = opendir($dir))
                        {
                            $cont = 0;
                            while (($file = readdir($gd)) !== false)
                                {
                                    if ($file != "." && $file != ".." )
                                        {
                                            if (is_dir($file))
                                                {
                                                    $cont += Fsize($dir."/".$file);
                                                }
                                            else
                                                {
                                                    $cont += filesize($dir."/".$file);
                                                    echo  "file : " . $dir."/".$file . "&nbsp;&nbsp;" . filesize($dir."/".$file)."<br />";
                                                }
                                        }
                                }
                            closedir($gd);
                        }
                }
            return $cont;
        }

?>

The size it shows me of the folder is 3891923, but it is not the real size, when validating the directory the real size is 191791104 bytes

Can you help me, please?

Armin
  • 15
  • 5

1 Answers1

0

Your test for directory is incorrect here:

if (is_dir($file))   // This test is missing the directory component
     {
     $cont += Fsize($dir."/".$file);
     }
else

Try:

if (is_dir("$dir/$file"))   // This test adds the directory path
     {
     $cont += Fsize($dir."/".$file);
     }
else

PHP offers a number of iterators that can simplify operations like this:

$path = "path/to/folder";
$Directory = new RecursiveDirectoryIterator($path);
$Iterator = new RecursiveIteratorIterator($Directory);
$Iterator->setFlags(FilesystemIterator::SKIP_DOTS);

$totalFilesize = 0;
foreach($Iterator as $file){
    if ($file->isFile()) {
        $totalFilesize += $file->getSize();
    }
}

echo "Total: $totalFilesize";
  • I understand, thanks for your support and for the documentation. I did some tests with the code and I see that there is timeout on the page while loading the data, is there a way to solve this timeout? Ajax could be used so that the load is immediate and does not delay. – Armin Feb 12 '21 at 04:01
  • Your code recurses down the directory tree without limitation. If there are a lot of files this will take time. You can raise the time limit with [set_time_limit()](https://www.php.net/manual/en/function.set-time-limit.php). Ajax won't help reduce the delay, but you could cache the result of you need it more than once. – Tangentially Perpendicular Feb 12 '21 at 05:06
  • I understand, with what you tell me it is not recommended to use ajax, although with cache it would be possible, then how would you recommend me to carry out this example: Storage `50 GB (14.12%) -> the size of the folder` of `353 GB -> the disk size` used, I'm using windows. – Armin Feb 12 '21 at 19:39
  • There's no answer to this. If you ask Windows to do this it will take time, sometimes a lot of time. File system activity is slow in computer terms. The best you can do is mitigate against it with caching, but then your numbers won't necessarily be accurate all the time. I'd be asking if you really need to do this at all. – Tangentially Perpendicular Feb 12 '21 at 20:05
  • I needed it to implement it on the web that I am doing, a university assignment, but if it is really difficult to query the space (because of the delay in response) then I will remove the storage example. Thank for the support and documentation. – Armin Feb 12 '21 at 21:43