Cleanup Cached Resized/Thumbnails on Update/Delete [SOLVED]
Hi,
This issue of being able to cleanup the thumbnails when an image is deleted is really important and does not seem to be getting much attention.
I managed to accomplish it with the code below, but I HAD to set the image name to a session variable and delete the files after the update was executed and the page redirected.
The resize functions exist on the update page showing a smaller version of the uploaded image above the FILE input field.
If I try and delete the thumbnails on the form submit, above the update transaction, the thumbnails and their directories delete, but are then recreated. I'm not sure if they are recreated before or after the redirect from the update transaction, but it is like the code inside the body tag is being executed before the update transaction redirects the page.
The code I used is as follows and must be placed above the update code.
<?php
function getDirectory($path = '.', $ignore = '') {
$dirTree = array ();
$dirTreeTemp = array ();
$ignore[] = '.';
$ignore[] = '..';
$dh = @opendir($path);
while (false !== ($file = readdir($dh))) {
if (!in_array($file, $ignore)) {
if (!is_dir("$path/$file")) {
$dirTree["$path"][] = $file;
} else {
$dirTreeTemp = getDirectory("$path/$file", $ignore);
if (is_array($dirTreeTemp))$dirTree = array_merge($dirTree, $dirTreeTemp);
}
}
}
closedir($dh);
return $dirTree;
}
/*
$ignore = array('.htaccess', 'error_log', 'cgi-bin', 'php.ini', '.ftpquota');
$dirTree = getDirectory('/your/path/here', $ignore);
*/
?>
<?php
// WA File Cleanup
if (isset($_SESSION['cleanup'])) {
$ignore = array('.htaccess');
$dirTree = getDirectory($_SERVER['DOCUMENT_ROOT'].'/image_cache/images/blogs', $ignore);
if (count($dirTree)) {
foreach ($dirTree as $dir => $files) {
$dirName = substr($dir,strrpos($dir,"/")+1,strlen($dir));
$filter = substr($_SESSION['cleanup'], 0, strrpos($_SESSION['cleanup'], '.'));
if (substr($dirName,0,strlen($filter)) == $filter) {
if (count($files)) {
foreach ($files as $file) {
unlink($dir."/".$file);
}
}
rmdir($dir);
}
}
}
unset($_SESSION['cleanup']);
}
if (($WA_DFP_UploadStatus["WA_UploadResult1"]["statusCode"] == 1) && ($_POST["exec"] != "") && ($row_WADArs['image'] != "")) // Trigger
{
$_SESSION['cleanup'] = $row_WADArs['image'];
}
?>