2011-03-17 71 views

回答

17

使用file_get_contents()加載文件將阻止腳本的運行,直到PHP完全讀完。它必須,因爲否則,您不能指定$content =

+2

+1 Yep。 PHP!= JS。 – NikiC 2011-03-17 21:32:09

1

PHP是單線程的 - 所有功能都是一個接一個地發生。如果你想嘗試異步加載文件,有一個php_threading PECL擴展,但我沒有自己嘗試過,所以我不能說它是否會起作用。

0

一個簡單的例子,將通過並得到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(); 
} 
?>