无法在 php ajax 中的 while() 循环中打印

发布于 2024-12-04 13:03:28 字数 464 浏览 0 评论 0原文

我试图通知用户,在后台运行的(另一个)脚本的状态(每分钟运行一次)。

所以我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-12-11 13:03:28

好吧,你正在阻止错误。

试试这个:

$fOpen = @fopen("dl.conf","r");

if(!$fOpen) die('ERROR of some kind...');

while($fOpen){ 
  print "Download will start soon";
  flush();
  sleep(1);
}

Well you are blocking the error.

Try this:

$fOpen = @fopen("dl.conf","r");

if(!$fOpen) die('ERROR of some kind...');

while($fOpen){ 
  print "Download will start soon";
  flush();
  sleep(1);
}
风蛊 2024-12-11 13:03:28

你最好在这种情况下使用ajax并更改它
应在 ajax onresponse 事件中处理的脚本

if(file_exists('dl.conf'))
{
 return 'file created, download will start soon';
}

you'd better to use ajax for this case and also change this
script which should be handled in ajax onresponse event

if(file_exists('dl.conf'))
{
 return 'file created, download will start soon';
}
怀念你的温柔 2024-12-11 13:03:28

您不需要使用 fopen() 来检查文件是否存在,您可以像这样简单地使用 file_exists()

// run loop till file exists
while(file_exists("dl.conf")){ 
    print "Download will start soon";
    flush();
    sleep(1);

    // clear stat cache to get correct file status
    clearstatcache();
}

如果后台脚本也是 PHP 脚本并使用 unlink() 删除 dl.conf 文件,则不需要clearstatcache() 调用。由于unlink()会自动清除文件统计缓存。

更新

有时,flush() 或 ob_flush 单独不起作用。看看 PHP 文档上的评论 http://php.net/manual/ en/function.ob-flush.php#90529

所以如果你确实使用了output_buffering,你可以尝试这组命令。

ob_end_flush(); 
ob_flush(); 
flush(); 
ob_start(); 

You do not need to use fopen() to check if file is there, you can simply use file_exists() like this.

// run loop till file exists
while(file_exists("dl.conf")){ 
    print "Download will start soon";
    flush();
    sleep(1);

    // clear stat cache to get correct file status
    clearstatcache();
}

You do not need the clearstatcache() call if the background script is also a PHP script and uses unlink() to delete the dl.conf file. As unlink() 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.

ob_end_flush(); 
ob_flush(); 
flush(); 
ob_start(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文