php 如何等待文件存在?
我正在使用异步 Web 服务,它会向我发送回作业 ID。然后,Web 服务使用 PUT 请求将完整响应作为 XML 文件 (jobId.xml) 发送到我的服务器。
向网络服务发出请求并获取 jobID 后,我 想知道最好(最有效)的等待方式是什么 直到文件被创建到我的服务器的 PUT 目录中?
我正在考虑一个 file_exist() 循环,在每次执行之间有一个 usleep() 并有一个超时。
但我确信有更好的方法。 像侦听器之类的东西...
文件可能需要不到一秒到超过一分钟的时间才能到达。
谢谢!
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 the 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
inotify
来监视某个目录中的文件创建。或者,您可以修改 PUT 处理程序以通知您的脚本。例如,您可以创建一个命名管道,从中读取(阻塞),然后修改 PUT 处理程序以在已写入文件的管道上写入
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
我不明白您是否需要 php 进程保持活动状态(而不是返回并死亡)。
如果没有,请考虑使用 incrontab。
它类似于 cronjob,但不是按预先配置的时间运行,而是根据文件更改运行。您可以将其配置为侦听目录,当文件在文件系统上创建完成时,incronjob 将触发您想要的程序。
这是针对 unix/linux 操作系统的解决方案。你没有提到你的操作系统。
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.