close ad
 
Important WebAssist Announcement
open ad
View Menu

Web development tutorial

Using a cron job to empty a folder's contents

Tutorial created by Jamie, Motley.co.uk

Categories: PHP

rating

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

arrow downCreating the 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!

arrow downAdd folders to be 'emptied'

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.';


?>

arrow downSave and upload the file

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!

arrow downSetting up the cron job

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!

arrow downReviews and comments

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 comments
rating
rating

this_is_me: 9 Years, 6 Months, 1 Week, 3 Days, 10 Hours, 53 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.

: 1 Year, 5 Months, 3 Weeks, 1 Day, 8 Hours, 5 Minutes ago

Thank you so much. This worked perfectly!!!

So happy. Hope you get reincarnated as a god!

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

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.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.