Beispiel #1
0
 public static function garbage_collector($chance = 5)
 {
     if (function_exists('mt_rand')) {
         $rand = mt_rand(0, 100);
     } else {
         $rand = rand(0, 100);
     }
     if (!Torn_Uploader::$collected and !Request::$is_ajax and $chance >= $rand) {
         Torn_Uploader::$collected = TRUE;
         // collect garbage
         $cache = Cache::instance();
         $i = 0;
         foreach (new DirectoryIterator(Kohana::$cache_dir) as $file) {
             $name = $file->getFilename();
             if ($file->isFile() and preg_match('/^[a-z0-9]{32}-[a-z0-9]{32}$/iD', $name)) {
                 $data = $cache->get($name);
                 if (!empty($data) and $data['timestamp'] < time()) {
                     try {
                         unlink($file->getPathname());
                         $cache->delete($name);
                         $i++;
                     } catch (Exception $e) {
                     }
                 }
             }
         }
         Kohana::$log->add('info', 'Torn Uploader garbage collector removed :items files', array(':items' => $i));
     }
 }