无法在 php ajax 中的 while() 循环中打印
我试图通知用户,在后台运行的(另一个)脚本的状态(每分钟运行一次)。
所以我有这个 php 代码:
while(@fopen("dl.conf","r")){ print "Download will start soon"; flush(); sleep(1); } . .
它不会打印任何内容,而且即使我取消该文件,它也不会执行脚本的其余部分。
基本上用户可以提交文件进行下载。这将触发创建文件 dl.conf。有一个脚本在后台运行,检查 dl.conf 是否存在,如果存在,则读取它并开始下载。它也会删除 dl.conf 文件。
可能会发生这样的情况:用户提交了下载,但脚本将需要几秒钟的时间才能再次运行,并意识到有东西要下载。所以我只是希望用户等待下载很快就会开始(然后将显示下载进度)。
谢谢
I am trying to notify a user, the status of a (another)script the runs in background (it runs once every minute).
So I have this php code:
while(@fopen("dl.conf","r")){ print "Download will start soon"; flush(); sleep(1); } . .
It doesn't print anything, plus even if I cancel that file it won't execute the rest of the script.
basically a user can submit a file to download. this will trigger the file dl.conf to be created. there is a script running in background checking if dl.conf exists, if so it reads it, and starts the download. It will delete dl.conf file too.
It might happen that the user submit the download but the script will take few more seconds to run again, and realize that there is something to download. so I just want the user to wait for the download which will start very soon (then the download progress will be shown).
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你正在阻止错误。
试试这个:
Well you are blocking the error.
Try this:
你最好在这种情况下使用ajax并更改它
应在 ajax onresponse 事件中处理的脚本
you'd better to use ajax for this case and also change this
script which should be handled in ajax onresponse event
您不需要使用
fopen()
来检查文件是否存在,您可以像这样简单地使用file_exists()
。如果后台脚本也是 PHP 脚本并使用
unlink()
删除dl.conf
文件,则不需要clearstatcache() 调用。由于unlink()
会自动清除文件统计缓存。更新
有时,flush() 或 ob_flush 单独不起作用。看看 PHP 文档上的评论 http://php.net/manual/ en/function.ob-flush.php#90529
所以如果你确实使用了output_buffering,你可以尝试这组命令。
You do not need to use
fopen()
to check if file is there, you can simply usefile_exists()
like this.You do not need the clearstatcache() call if the background script is also a PHP script and uses
unlink()
to delete thedl.conf
file. Asunlink()
clears the file stat cache automatically.Update
Sometimes flush() or ob_flush alone do not work. Look at this comment on PHP documentation http://php.net/manual/en/function.ob-flush.php#90529
So if you are using output_buffering for sure, you can try this set of commands.