I’ve set us a system to allow authorized visitors to play private music files. The script essentially creates a temporary directory with a unique name for an authorized user, where the requested music file is placed. (It even builds an .htaccess file to help prevent others from accessing the temp files.). But I need a way to get rid of the temp files and directories after they’re no longer needed. Unfortunately the script cant “sleep() or wait around for time to pass. It is called via an XMLHttpRequest in SYNCHRONOUS mode because it must finish and return a result before the calling page can play the music. So the only thing missing is a way to stop the temp files from hanging around forever.
So since the script must compete and return a result, is it possible for the script to start a 2nd PHP program, passing it the private directory? That second program could then stay in a loop, sleeping for a minute at a time, while checking the last mod date on the directory. When it sees the directory hasn’t been “touched” after a certain time, it can then delete and exit. Essentially a one-time garbage collector for the current user.
So my first question is, is this even possible? How would a PHP script (called from a web page) start another PHP script, without “blocking” or waiting for this 2nd script to exit?
The second question is, if the above is possible, am I doing it a stupid way? I could set up a Cron job I guess, which could look at all directories and files in the “temporary” area, and delete then when they get old. But I just thought that was overkill, since the Chron job would be running all the time, while the first method I’m considering would only run on an “as needed” basis.
Thanks for any thoughts!