NuBOXDevCom /
Database-Backup
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace NDC; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class FileManager |
||
| 9 | * @package NDC |
||
| 10 | */ |
||
| 11 | class FileManager |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | private array $params; |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private array $files = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * FileManager constructor. |
||
| 24 | */ |
||
| 25 | public function __construct() |
||
| 26 | { |
||
| 27 | (new System())->loadConfigurationEnvironment(); |
||
| 28 | $this->params = [ |
||
| 29 | 'files_path' => $_ENV['FILES_PATH_TO_SAVE_BACKUP'], |
||
| 30 | 'days_interval' => (int)$_ENV['FILES_DAYS_HISTORY'] |
||
| 31 | ]; |
||
| 32 | $this->_getFiles(); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | private function _getFiles(): void |
||
| 39 | { |
||
| 40 | $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING); |
||
| 41 | $this->files = array_filter( |
||
| 42 | $files, |
||
| 43 | function ($k) { |
||
| 44 | $return = []; |
||
| 45 | if ($k !== '..' && $k !== '.') { |
||
| 46 | $return[] = $k; |
||
| 47 | } |
||
| 48 | return $return; |
||
| 49 | } |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function removeOldFilesByIntervalDays(): void |
||
| 57 | { |
||
| 58 | $time_before = time() - $this->params['days_interval'] * 86400; |
||
| 59 | foreach ($this->files as $file) { |
||
| 60 | $f = explode('-', $file); |
||
| 61 | $f = str_replace('.sql', '', $f); |
||
| 62 | if ($f[1] < $time_before) { |
||
| 63 | @unlink($this->params['files_path'] . '/' . $file); |
||
|
0 ignored issues
–
show
|
|||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 |
If you suppress an error, we recommend checking for the error condition explicitly: