Tell me please! How to implement folder sizing using php.
There is a file sharing service, with the help of which people upload files to the server. When I send a file to the server, I want to run a script that determines the size of the folder where everything falls, and if the total size exceeds 1GB, then a letter is sent to me by mail.

    2 answers 2

    function getFilesSize($path) { $fileSize = 0; $dir = scandir($path); foreach($dir as $file) { if (($file!='.') && ($file!='..')) if(is_dir($path . '/' . $file)) $fileSize += getFilesSize($path.'/'.$file); else $fileSize += filesize($path . '/' . $file); } return $fileSize; } 

    a source

    • Ohhh Eeee! Thank you :) - archisova

    Through SPL is a bit simpler.

     $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator("/path/to/dir", FilesystemIterator::SKIP_DOTS) ); $size = 0; foreach ($it as $fi) { $size += $fi->getSize(); } echo $size; 
    • Cool! Thank you :) - archisova