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); } 
  • for the next removal you need to refresh the page. Perhaps unfortunately PHP does not work dynamically ... - Palmervan

2 answers 2

You declare the function in the loop, the next cycle step is Fatal Error: Cannot redeclare getFilesSize But you don’t see this error, then it’s not included

 error_reporting(E_ALL); ini_set('display_errors', 1); 

Let's go further.

  $fileSize = (int)((getFilesSize()/1024)/1024); // Округление до Мбайтов } while ($fileSize <= $maxFileSize); 

$maxFileSize rounded to megabytes too?

Here is the sample code, which should be:

 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; // Возвращает значение "размер файлов в байтах" } function clearFiles($path, &$curSize, $limit) { $dir = opendir($path); while ($e = readdir($dir)) { if ($curSize <= $limit) { return true; } if (substr($e, 0, 1) == '.') { continue; } $fullPath = $dir.'/'.$e; if (is_dir($fullPath)) { clearFiles($fullPath, $curSize, $limit); } elseif (is_file($fullPath)) { $curSize -= filesize($fullPath); if (unlink($fullPath)) { echo "<span style='text-align: center; color: green;'>Файл: $fullPath - удален!</span>"; } else { echo "<span style='text-align: center; color: red;'>Не могу удалить: $fullPath</span>"; } } } } //$maxFileSize = думаю выше он где то определен $fileSize = getFilesSize(); if($fileSize > $maxFileSize){ clearFiles('storage', $fileSize, $maxFileSize); } 
  • >> and what does & in front of the variable name $ curSize mean? transfer by reference. Changing the function argument inside the function, it also changes. - Alex Kapustin
  • Cheto nothing works! Falling on something! On what I can not understand: ( - archisova

It is better to make the function of determining the size of the folder out of the loop, since it will be more convenient to be perceived firstly, and secondly it will cease to seem that the error is somewhere there.

 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 = getFilesSize(); // возможно, у вас это уже есть. 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>"; $fileSize = floor((getFilesSize()/1024)/1024); // Округление до Мбайтов } while ($fileSize <= $maxFileSize); } 
  • This has already been done, on the contrary, I pushed into a cycle so that it pushed a new value into the condition. - archisova
  • But before the if ($ fileSize> $ maxFileSize) condition, are the $ filesize and $ maxFileSize variables initialized? - AseN