Jump to content

How to automatically delete files from a directory that are older than 5 days?


blurredvision

Recommended Posts

I have a directory that is being filled by temporary text files that are created by a form and some php.  These text files are used once at the time of creation, then not used again.  I'm wanting to put in some code so that every time someone visits this page, it will look at that directory and delete all the files older than 5 days, basically to keep the directory clean and clutter-free without having to manually go in and delete files every now and then.

 

Now, I don't want to be told how exactly to do this, I just need some direction so I can figure out how best to do this, otherwise I'll never learn.  Can you guys point me in the right direction of a php built-in function to where I can read and compare file creation dates, or is there some other method that would work best for me?

 

Thanks a lot!

Link to comment
Share on other sites

is this a linux or a windows server and do you own it?  This type of task is MUCH better suited for a system daemon running in the background.  What happens if you get 10,000 visitors to the page in 2 minutes?  that's a lot of unnecessary calls to the function.

 

If it's a linux system, just google cron and file clean up.  It's literally a 1 line command.  If it's windows, write a batch file and schedule it.

 

as was mentioned before, it can be done with php server calls too (look up exec() and system() ), but it's really not the best way to go about it.

 

It sounds as if you are using a flat file database system for temporary storage.  There might also be a more elegant means to accomplish the task without the use of temp files.  What exactly is the script doing?

Link to comment
Share on other sites

create the php script so that it will run from the command line (test it first).

 

go to start -> control panel -> scheduled tasks.

 

add a new task.  In the the task properties is a 'run' box.  Enter the full path to the php executable and to the script with any needed arguments.

 

in the batch file option, you would just write a batch file to do the search and delete and ignore php completely.  Not as easy as the linux shell script, but still doable.  In fact, if you google around a bit, you should be able to find some compiled scripts that will do this.  I've been in the linux environment so long now that no names come to mind.

Link to comment
Share on other sites

Sorry guys, I should've explained better.  The page that creates the text file is redirected to another page that automatically runs a program (this program uses the text file to run).  This destination page is administered by another person, and he tells me that the way he has his page set up (it runs a myriad of programs), he can't easily have it delete the text file after the program is done running.  The text file that is created cannot be deleted until after the program is complete, and since my initial page that creates the text file is completely out of the picture at this point, I don't know when the program is done.  Plus, there's always the rare chance that a user might need to take a look at the text file in case something goes wrong with the program, but if it's been longer than 5 days, then they won't need it.

 

So, I've decided to take care of this on my end instead of having the other guy putting time towards deleting the file after the program has run (he has more important stuff to do right now).  This isn't a do or die kinda thing, just one of those things that needs to be done at some point to automate everything so a user doesn't have to do anything but run the program.

 

As far as running some kind of cron job or daemon, the server admin does not want any crons running on the sever we are using.  There's a particular reason, but it's a work thing, not important to the task at hand.  Plus, only a few people use this script.  In the 3 weeks the script has been live, only 175 text files have been created, and I'd say half of those were test runs when I first launched the page.

 

This is on a linux box.

Link to comment
Share on other sites

if it's a low load server like you say, just take the same function you'd use in the cron job and stick it in an exec() block on either page.  If it's deleting file older then 5 days, you should have no issue interfering with the other app. 

 

you can use "find" on a linux box to get the list of files older (or newer) then x days

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.