I need to delete files from a directory in my web-server and I need some script to do it automatic. And there is few more conditions which need to be fulfilled.
- It has to clean the directory then total file size exceeds some certain limit for example 1,5GB
- That files would be deleted every 24 hours.
- New files like 10 ir 20 mins old would not be deleted
- It would be just awesome if all of the conditions above would be work at the same time.
For deleting files I was thinking of some kind of corn task but I do not know a thing about them so it is tricky for me.
To delete files then a certain limit is reached I was thinking that some kind of PHP script my help. May be there is a library meant for these things.
Any way I will be very happy for any kind of help. May be I just need some fresh thoughts to come up with a right keyword for google ;P
EDIT: My server runs on linux.
Here is an easy way to automatically perform a file deletion depending on the file date : Webmastips : Tips and Tools for webmasters : Automatic File Deletion triggered by a specific date You can customize and automate this task in order to fit your needs. Regards,
If your server is running linux, a
cron
job is the best option. Simply write a script that checks the conditions you mention (using e.g.stat
and some other tools) and deletes the file if they apply, and setcrond
to run it every 24 hours. Here's a short introduction tocron
.On Windows, you'd do something similar with a script and a scheduled task.
Here is a good cron job tutorial and explanation:
http://www.youtube.com/watch?v=lbJrk55Ae1c
Automate any script to execute. Just write a script that removes all files in folder and that is what you set your cron job to target.
And here is a tutorial about how a PHP programmer can access all files in a folder and use the php unlink() function on each in succession to delete the files. unlink($file);
http://www.developphp.com/view_lesson.php?v=239
Go to your cPanel, select cron jobs and use the following command to create it:
It will clean the "temp" (or any other name) file every 360 seconds. You can change that as well.
I am told that if you add the following to the end of the file it will prevent you from getting e-mails every time the cron job executes:
/dev/null 2>&1