blurredvision Posted July 2, 2008 Share Posted July 2, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/ Share on other sites More sharing options...
ag3nt42 Posted July 2, 2008 Share Posted July 2, 2008 if there is no reason to keep the files for 5 days then just delete them once you've finished using them.. problem solved. php filesystem functions Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579953 Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579959 Share on other sites More sharing options...
DMComics Posted July 2, 2008 Share Posted July 2, 2008 um...Interesting stuff. How does one schedule a .php through windows batch files? Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579960 Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579970 Share on other sites More sharing options...
blurredvision Posted July 2, 2008 Author Share Posted July 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579972 Share on other sites More sharing options...
ag3nt42 Posted July 2, 2008 Share Posted July 2, 2008 why can't he just redirect the user to another page and delete the file? its like 1 line of code Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579979 Share on other sites More sharing options...
mbeals Posted July 2, 2008 Share Posted July 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/112909-how-to-automatically-delete-files-from-a-directory-that-are-older-than-5-days/#findComment-579991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.