Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working.
This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser.
Things to consider if you are courious but not mandatory:
- Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images;
- if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again.
- It might take a long time so if You will run out of time then just run the script again.
- Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You.
- Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free.
-
This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit
<?php ####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER#### // root path of the shop, almost no one needs to change something here. $shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end $image_folder = 'img/p/'; // also needs slash at the ennd $scan_dir = $shop_root.$image_folder; include_once($shop_root.'config/config.inc.php'); include $shop_root . 'config/settings.inc.php'; #---------------------------------------------# $last_id = (int)Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC '); $counted_images = Db::getInstance()->executeS(' SELECT count(*) as qnt FROM '._DB_PREFIX_.'image '); $counted_images = (int)$counted_images[0]['qnt']; echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>'; //$limit = 150; // for testing $limit = $last_id; // for production $removed_images = 0; for ($i=1; $i <= $limit; $i++) { if (!imageExistsInDB($i)){ $imageDir = str_split($i); $imageDir = implode('/', $imageDir); $path = $scan_dir.$imageDir; deleteImagesFromPath($path); } } function deleteImagesFromPath($path) { global $removed_images; $images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE); if ($images){ foreach ($images as $file) { if (is_file($file)) { unlink($file); } } $removed_images++; echo 'Deleted images from folder ' . $path . '/' ."<br/>"; } } function imageExistsInDB($id_image){ return Db::getInstance()->getValue(' SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image ); } echo '--------------------------------------<br>'; if ($removed_images > 0) echo 'Hurray! We removed '.$removed_images.' product images!'; else echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!';