From bb300c5d38285ea54a0d898023f24ca5d180ea9b Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Mon, 19 Aug 2013 01:33:30 +1000 Subject: [PATCH] tools/delete-stray-images.php --- tools/delete-stray-images.php | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tools/delete-stray-images.php diff --git a/tools/delete-stray-images.php b/tools/delete-stray-images.php new file mode 100644 index 00000000..cf0dff46 --- /dev/null +++ b/tools/delete-stray-images.php @@ -0,0 +1,61 @@ +#!/usr/bin/php +fetch(PDO::FETCH_ASSOC)) { + $valid_src[] = $post['file']; + $valid_thumb[] = $post['thumb']; + } + + $files_src = array_map('basename', glob($board['dir'] . $config['dir']['img'] . '*')); + $files_thumb = array_map('basename', glob($board['dir'] . $config['dir']['thumb'] . '*')); + + $stray_src = array_diff($files_src, $valid_src); + $stray_thumb = array_diff($files_thumb, $valid_thumb); + + $stats = array( + 'deleted' => 0, + 'size' => 0 + ); + + foreach ($stray_src as $src) { + $stats['deleted']++; + $stats['size'] = filesize($board['dir'] . $config['dir']['img'] . $src); + if (!file_unlink($board['dir'] . $config['dir']['img'] . $src)) { + $er = error_get_last(); + die("error: " . $er['message'] . "\n"); + } + } + + foreach ($stray_src as $thumb) { + $stats['deleted']++; + $stats['size'] = filesize($board['dir'] . $config['dir']['thumb'] . $thumb); + if (!file_unlink($board['dir'] . $config['dir']['thumb'] . $src)) { + $er = error_get_last(); + die("error: " . $er['message'] . "\n"); + } + } + + echo sprintf("deleted %s files (%s)\n", $stats['deleted'], format_bytes($stats['size'])); +}