【问题标题】:Check when file_get_contents is finished检查 file_get_contents 何时完成
【发布时间】:2011-03-17 21:28:42
【问题描述】:

无论如何我可以检查 file_get_contents 何时完成加载文件,以便我可以加载另一个文件,它会在继续加载下一个文件之前自动完成加载一个文件吗?

【问题讨论】:

标签: php file-get-contents


【解决方案1】:

使用file_get_contents() 加载文件将阻止您的脚本操作,直到 PHP 完全读完它。必须,因为否则您无法分配 $content =

【讨论】:

    【解决方案2】:

    PHP 是单线程的——所有函数一个接一个地发生。如果您确实想尝试异步加载文件,有一个 php_threading PECL 扩展,但我自己没有尝试过,所以我不能说它是否有效。

    【讨论】:

      【解决方案3】:

      一个简单的例子,它将循环并获取 google.co.uk#q=* 5 次并输出它是否得到它,非常没用,但有点回答你的问题,可以检查 file_get_contents 是否成功在做下一个之前,显然谷歌可以改成别的东西。但不会很实用。加上输出缓冲不在函数内输出。

      <?php 
      function _flush (){
          echo(str_repeat("\n\n",256));
          if (ob_get_length()){           
              @ob_flush();
              @flush();
              @ob_end_flush();
          }   
          @ob_start();
      }
      function get_file($loc){
          return file_get_contents($loc);
      }
      
      for($i=0;$i<=5;$i++){
      
          $content[$i] = @get_file("http://www.google.co.uk/#q=".$i);
          if($content[$i]===FALSE){
              echo'Error getting google ('.$i.')<br>';
                  return;
          }else{
              echo'Got google ('.$i.')<br>';
          }
          ob_flush();
          _flush();
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-07
        • 2023-03-22
        • 1970-01-01
        • 2012-03-13
        相关资源
        最近更新 更多