Madproject

Always know where your towel is.

Photostore Purge Plugin

I recently had to purge about 100’000 photos from a Photostore site and doing it using the manager control panel is very time consuming. I developed a little plugin that will delete all photos not associated to a category and clean up DB entries. Worked great for me but use at your own risk.

<?php

/********************************************************/
/*   Photostore mass photo purge                        */
/*   Author: Trevor Wistaff                             */
/*   Site: http:/www.madproject.com						*/
/*	 Email: easycoatgmaildotcom							*/
/*   Date: 17.11.2010                                   */
/*													    */
/* This little chunk of code was developed out of       */
/* necessity. I needed to purge large amounts of        */
/* photos and I had no quick way of doing it.           */
/*													    */
/* How to use: Delete the categories containing the     */
/* photos you wish to purge. Manager->Categories.       */
/* The photos will then be listed in the Batch Edit		*/
/* section of the manager control panel. Upload this	*/
/* file in the manager folder of your photostore then 	*/
/* run it directly from you browser:					*/
/* http://www.yoursite.com/photostore/manager/purge.php */
/*                                                      */
/* Legal mumbo jumbo: Use this tool at your own risk.   */
/* I've only tested it on my own site and can't say how */
/* it might work on a different server and version.     */
/* 														*/
/* This plugin was developed for:						*/
/* Ktools.net PhotoStore Version 3.8.3					*/
/*														*/
/* Thanks and stay classy 								*/
/********************************************************/

// SETTINGS
// DATABASE
define('DBHOST','localhost'); //HOST
define('DBUSER','user'); //USER
define('DBPASS','password'); //PASSWORD
define('DBNAME','database_name'); //NAME

// IMAGE PATH
define('IMAGE_PATH','../stock_photos/');  //SHOULDN'T NEED TO BE CHANGED

// CONNECTION
$link = mysqli_connect(DBHOST, DBUSER, DBPASS);

if (!$link) {
	echo 'Unable to connect to the database server.';
	exit();
}
if (!mysqli_set_charset($link, 'utf8')) {
	echo 'Unable to set database connection encoding.';
	exit();
}

if (!mysqli_select_db($link, DBNAME)) {
	echo 'Unable to locate the ' . DBNAME . ' database.';
	exit();
}

// START PURGE

// SELECT ALL CATEGORIES NOT ASSIGNED TO CAT
$result_1 = mysqli_query($link, "SELECT id FROM photo_package WHERE gallery_id = ''");
if (!$result_1) {
	echo mysqli_error($link);
	exit();
}

while($photo_package = mysqli_fetch_assoc($result_1)) {

	// SELECT PHOTO MATCHING PHOTO_PACKAGE.ID == UPLOADED_IMAGES.REFERENCE_ID
	$result_2 = mysqli_query($link, "SELECT id, filename FROM uploaded_images WHERE reference_id = '{$photo_package['id']}'");
	if (!$result_2) {
		echo mysqli_error($link);
		exit();
	}

	while($uploaded_images = mysqli_fetch_assoc($result_2)) {

		// ECHO IMAGE DETAILS
		echo IMAGE_PATH . $uploaded_images['filename'] . "<br />";
		echo IMAGE_PATH . "PhotoID: " . $uploaded_images['id'] . " - PackageID: " . $photo_package['id'] . "<br />";

		//DELETE PHYSICAL PHOTOS
		if(file_exists(IMAGE_PATH . $uploaded_images['filename'])){
			if(unlink(IMAGE_PATH . $uploaded_images['filename'])) {
				echo "<div style=\"color: #00cc33\">image deleted successfully</div>";
			} else {
				echo "<div style=\"color: #ff0000\">image deleted unsuccessfully</div>";
			}
		}
		if(file_exists(IMAGE_PATH . "i_" . $uploaded_images['filename'])){
			if(unlink(IMAGE_PATH . "i_" . $uploaded_images['filename'])) {
				echo "<div style=\"color: #00cc33\">i_ image deleted successfully</div>";
			} else {
				echo "<div style=\"color: #ff0000\">i_ image deleted unsuccessfully</div>";
			}
		}
		if(file_exists(IMAGE_PATH . "s_" . $uploaded_images['filename'])){
			if(unlink(IMAGE_PATH . "s_" . $uploaded_images['filename'])) {
				echo "<div style=\"color: #00cc33\">s_ image deleted successfully</div>";
			} else {
				echo "<div style=\"color: #ff0000\">s_ image deleted unsuccessfully</div>";
			}
		}
		if(file_exists(IMAGE_PATH . "m_" . $uploaded_images['filename'])){
			if(unlink(IMAGE_PATH . "m_" . $uploaded_images['filename'])) {
				echo "<div style=\"color: #00cc33\">m_ image deleted successfully</div>";
			} else {
				echo "<div style=\"color: #ff0000\">m_ image deleted unsuccessfully</div>";
			}
		}

		//DELETE DATABASE ENTRIES
		$result_3 = mysqli_query($link, "DELETE FROM uploaded_images WHERE id = '{$uploaded_images['id']}'");
		if(!$result_3) {
			echo "<div style=\"color: #ff0000\">" . mysqli_error($link) . "</div>";
			exit();
		} else {
			echo "<div style=\"color: #00cc33\">image delete successfully from uploaded_images table</div>";
		}
		$result_4 = mysqli_query($link, "DELETE FROM photo_package WHERE id = '{$photo_package['id']}'");
		if(!$result_4) {
			echo "<div style=\"color: #ff0000\">" . mysqli_error($link) . "</div><br />";
			exit();
		} else {
			echo "<div style=\"color: #00cc33\">image delete successfully from photo_package table</div><br />";
		}
	}
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *