Tell me please! There is (below code), it should work like this: if $ fileSize (the total weight of all files in the storage folder) exceeds the maximum allowable value of $ maxFileSize, then delete files in the storage folder until $ fileSize becomes less than or equal to $ maxFileSize.
The cycle itself runs only once, i.e. deletes one file, for the next removal you need to refresh the page.
if($fileSize > $maxFileSize){ do{ if (unlink("storage/$oldFile")) echo "<span style='text-align: center; color: red;'>Файл: $oldFile - удален!</span>"; else echo "<span style='text-align: center; color: red;'>Папка <b>storage</b> пуста!</span>"; function getFilesSize(){ // Функция определения размера папки $path = 'storage'; $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; // Возвращает значение "размер файлов в Кбайтах" } $fileSize = (int)((getFilesSize()/1024)/1024); // Округление до Мбайтов } while ($fileSize <= $maxFileSize); }