If, like me, you may have found that sometimes you find you have image cache folders getting clogged up with image files that are no longer used, then automate the deletion process using a cron job and script
This is the code for the script that you will be setting the cron job to run.
<?php
define('PATH', 'images/folder1/');
function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
else
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
closedir($mydir);
}
destroy(PATH);
echo 'all done.';
?>
Modify the line 'images/folder1/' to point to the folder you wish to empty of content (it doesn't delete the folder just the contents)
Just add that code script to a blank php file and save it - I name mine delete.php so I can tell quickly what it does!
If you want to empty multiple folders, just add more define('PATH', 'images/folder2/'); lines and adjust accordingly like this
<?php
define('PATH', 'images/folder1/');
define('PATH', 'images/folder2/');
define('PATH', 'images/folder3/');
function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
else
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
closedir($mydir);
}
destroy(PATH);
echo 'all done.';
?>
Just add that code script to a blank php file and save it - I name mine delete.php so I can tell quickly what it does!
Go to your cpanel for the site and got to the cron section and add a cron job - I run this one once a day and typically set it to run in the wee hours. I set mine to run at 3:41 am (if you're on a shared server, other resources are typically being run by other sites on the hour or half hour so this reduces your cron job from not running)
The path you need to run the cron will depend on your host but this is one of mine so you can see the structure
/usr/bin/php /home/ybgorrvn/public_html/delete.php
Thats it!
Comments will be sent to the author of this tutorial and may not be answered immediately. For general help from WebAssist, please visit technical support.
Sign in to add commentsYour friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.
this_is_me: 10 Years, 3 Months, 4 Weeks, 2 Days, 20 Hours, 55 Minutes ago
Thanks,
a most useful contribution and solution to a very annoying problem, indeed.
As a little suggestion may I add to save the content of the directory first (e.g. via ftp) to a local media. It always happens to me that after a deletion I realize that one or two pictures should have been saved before.
Thanks again, I appreciate your efforts to share this.
R.
: 2 Years, 3 Months, 1 Week, 4 Days, 18 Hours, 6 Minutes ago
Thank you so much. This worked perfectly!!!
So happy. Hope you get reincarnated as a god!