From f5bc76ce2fd240325ffc2d0a5e71a27231de2149 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 19 Feb 2012 17:49:00 +0100 Subject: file/cron: clean up stale files Signed-off-by: Florian Pritz --- application/controllers/file.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/application/controllers/file.php b/application/controllers/file.php index f578dd7c7..c83805600 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -281,6 +281,39 @@ class File extends CI_Controller { } } } + + /* remove files without database entries */ + // TODO: put into a function + $upload_path = $this->config->item("upload_path"); + $outer_dh = opendir($upload_path); + + while (($dir = readdir($outer_dh)) !== false) { + if (!is_dir($upload_path."/".$dir) || $dir == ".." || $dir == ".") { + continue; + } + + $dh = opendir($upload_path."/".$dir); + + $empty = true; + + while (($file = readdir($dh)) !== false) { + if ($file == ".." || $file == ".") { + continue; + } + + $query = $this->db->query("SELECT hash FROM files WHERE hash = ? LIMIT 1", array($file))->row_array(); + + if (empty($query)) { + unlink($upload_path."/".$dir."/".$file); + } else { + $empty = false; + } + } + + if ($empty) { + rmdir($upload_path."/".$dir); + } + } } } -- cgit v1.2.3-24-g4f1b