Skip to content

Instantly share code, notes, and snippets.

@eapdob
Created June 10, 2019 20:55
Show Gist options
  • Save eapdob/3d02bee0ed6ff0cb02a117657bacb3c1 to your computer and use it in GitHub Desktop.
Save eapdob/3d02bee0ed6ff0cb02a117657bacb3c1 to your computer and use it in GitHub Desktop.
script for removing lost photos in opencart
public function scan_Dir($dir) {
$dir = str_replace("//", "/", $dir);
$arrfiles = array();
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
chdir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
$arr = $this->scan_Dir($file);
foreach ($arr as $value) {
$arrfiles[] = $dir."/".$value;
}
} else {
$arrfiles[] = $dir."/".$file;
}
}
}
chdir("../");
}
closedir($handle);
}
return $arrfiles;
}
echo "<pre>";
$time_pre = microtime(true);
$net = 0;
$find_files = $this->scan_Dir(DIR_IMAGE . "data"); // здесь указываем директория где есть только фото товаров
if($find_files) {
foreach($find_files as $file){
$image = explode("www/image/", $file);
$query = $this->db->query("SELECT product_id FROM oc_product WHERE image = '".$image[1]."'"); //главное фото
if(!$query->rows){
$query_dop = $this->db->query("SELECT * FROM oc_product_image WHERE image = '".$image[1]."'"); //доп фото
if(!$query_dop->rows){
echo $file . "<br>"; //можно раскомментировать строку - отображение файла который удаляется
//unlink($file);
$net++;
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
if ($exec_time > 10 * 1000000) break;
}
}
}
}
echo "Всего фото: " . count($find_files) . "<br>";
echo "Удалено: " . $net . "<br>";
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment