How to wait for a file to exist with php?
I am using an asynchronous webservice which send me back a Job ID. The full response is then send by the webservice as an XML file (jobId.xml) to my server using a PUT request.
After issuing the request to th开发者_开发技巧e webservice and getting the jobID, I would like to know what would be the best (most efficient) way to wait until the file is created into the PUT directory of my server?
I am thinking of a loop of file_exist() with a usleep() between each execution and with a timeout.
But I am sure that there is a better way. Something like a listener...
The file can take less than a second to more than 1 minute to arrive.
Thanks!
You could use
inotify
to watch for file creations in some directory.Alternatively you could modify your PUT handler to notify your script. For example, you could create a named pipe, read from it (blocking), and modify your PUT handler to write on the pipe one it has written the file
I didn't understand if you need the php process to remain active (not return and die) or not.
If not, consider using incrontab.
It is like a cronjob but instead of running on preconfigured times, it is run according to file changes. You can configure it to listen to a directory, and when a file finished creating on the filesystem, the incronjob will fire your desired program.
This is a solution for a unix/linux OS. You did not mention your OS.
精彩评论