【问题标题】:Read timeout limit for file_get_contents with text file使用文本文件读取 file_get_contents 的超时限制
【发布时间】:2014-05-07 00:47:57
【问题描述】:

我正在使用file_get_contents,文件是本地文本文件。我想给它加一个超时条件。

目前我只有这个:

$fileData= file_get_contents($localFile); 

根据define a timeout

$timeout = array('http' => array('timeout' => 1));

$context = stream_context_create($timeout);

$file = file_get_contents('some url',false,$context);

如果源实际上是本地文件,我如何实现 timeout(context) 数组?

编辑: 为什么我需要这个?好吧,在我重新阅读我的问题后,我意识到我错过了目的,我正在尝试获取文件的内容,只要该文件在现在的超时秒内是可读的。

【问题讨论】:

  • 如何让本地文件超时?
  • 我很困惑......你为什么想要这个? :s

标签: php timeout file-get-contents


【解决方案1】:

如果你做了这样的事情会怎样

$startTime = microtime(true);
while($file === false && ((microtime(true) - $startTime) < 1)){
    $file = file_get_contents('some url');
}

【讨论】:

  • 你能解释一下这到底是做什么的吗?对不起,我是初学者。
  • 您以秒为单位获取时间,然后将您的 while 循环设置为只要您没有读取文件或没有超过执行时间就继续读取文件。注意:我还没有尝试过这个,甚至看看它是否会起作用。
  • 是的,那没用。 $startTime = microtime(true); while($data === false &amp;&amp; ((microtime(true) - $startTime) &lt; $timeout)){ $data = file_get_contents($localFile); } return($data);
  • By没用什么没用,没返回你的数据,是不是报错了?我还必须注意,我认为你会在达到时间限制之前用完分配的内存,除非你说的是百分之一秒
  • 不工作是指它没有返回任何数据。我假设这意味着读取超过了最大执行时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2023-04-07
相关资源
最近更新 更多