1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace Gregwar\Cache;
- class GarbageCollect
- {
-
- public static function dropOldFiles($directory, $days = 30, $verbose = false)
- {
- $allDropped = true;
- $now = time();
- $dir = opendir($directory);
- if (!$dir) {
- if ($verbose) {
- echo "! Unable to open $directory\n";
- }
- return false;
- }
- while ($file = readdir($dir)) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $fullName = $directory.'/'.$file;
- $old = $now-filemtime($fullName);
- if (is_dir($fullName)) {
-
- if (static::dropOldFiles($fullName, $days, $verbose)) {
- self::drop($fullName, $verbose);
- } else {
- $allDropped = false;
- }
- } else {
- if ($old > (24*60*60*$days)) {
- self::drop($fullName, $verbose);
- } else {
- $allDropped = false;
- }
- }
- }
- closedir($dir);
- return $allDropped;
- }
-
- public static function drop($file, $verbose = false)
- {
- if (is_dir($file)) {
- @rmdir($file);
- } else {
- @unlink($file);
- }
- if ($verbose) {
- echo "> Dropping $file...\n";
- }
- }
- }
|