【问题标题】:While and file_exists in PHPPHP 中的 while 和 file_exists
【发布时间】:2018-12-20 23:06:18
【问题描述】:

我在 php 上有这段代码:

while (!file_exists(test.txt)) sleep(1); //wait for test.txt (in the same path)
header("Location:loading.php"); //proceed with the next page

但它不起作用。

即使文件正确生成,它也会保持循环。

我只希望他找到文件后,继续使用标题,但直到那时才继续。

谢谢。

【问题讨论】:

  • 也许你忘记了引号? file_exists('test.txt')
  • 您可能还需要提供完整的路径名。
  • 你在这里的可怕代码;)
  • 经过测试,但问题仍然存在。谢谢。
  • 您需要提供与执行此代码的 php 相关的完整文件路径。否则任何人都在猜测。

标签: php while-loop header file-exists


【解决方案1】:

您需要将文件名用引号引起来,以便file_exists 函数能够正确读取它。您可能还需要指定文件的完整路径。

试试这个方法:

while (!file_exists('/path/to/test.txt')) sleep(1); //wait for test.txt (in the same path)
header("Location:loading.php"); //proceed with the next page

【讨论】:

  • 已测试,但问题仍然存在。谢谢。
  • 您确定文件是在执行时限内创建的吗?
【解决方案2】:

如果我这样做:

   if (!file_exists('test.txt')){ 
    usleep(7000000); 
    } 
    else { 
    header("Location:loading.php"); 
    } 
header("Location:loading.php");

它有效,但我必须等待 7 秒...我想要 go header,当文件存在时(没有固定的等待时间)。但是用了一会儿,它就不起作用了。

【讨论】:

  • 这只会执行一次检查。如果该文件在 7 秒内不存在,则无论该文件当时是否存在,它都会继续。
  • 我知道。出于这个原因,我想要一个循环来检查它。
猜你喜欢
  • 2011-02-14
  • 2014-01-19
  • 2016-04-29
  • 2011-01-03
  • 2016-01-23
  • 2011-10-04
  • 2011-10-02
  • 2017-11-09
  • 2011-04-04
相关资源
最近更新 更多