2013-05-21 63 views
1

我試圖從讀卡器捕獲實時饋送並使用PHP打印它。瀏覽器結束PHP無限循環,然後打印數據

<?php 
    $i=0; 
    for(;;) 
    { 
     $subdata=file_get_contents("/home/openflow/subscribedata.txt"); 
     $subdata2=file_get_contents("/home/openflow/subscribedatatemp.txt"); 

     if($subdata!=$subdata2) 
     { 
      copy("/home/openflow/subscribedatatemp.txt","/home/openflow/subscribedata.txt"); 
      $sub=file_get_contents("/home/openflow/subscribedata.txt"); 
      $i++; 
      echo "\n". $i."--".$sub; 
     } 
    } 
?> 

我使用for循環作爲無限循環。每當有新的數據,我的卡讀寫器的腳本將其寫入subscribedatatemp.txt文件和subscribedatatemp.txt(最新的條目)和subscribedata.txt之間的差異上面的腳本檢查(以前的條目) 。如果有差異,它應該將最新版本複製到前一版本並回顯最新數據。

問題是當我執行上面的PHP代碼時,它不會顯示任何內容,並且在某段時間瀏覽器停止加載並顯示加載時獲得的所有數據。

這表示循環執行正在停止,並且在while循環結束後打印所有數據,對嗎?我該如何解決這個問題?

+0

你的腳本可能是超時,嘗試添加'set_time_limit(0)'到頂部 – doublesharp

+0

我應該在那裏添加?我是新來的PHP,我不知道在哪裏添加該行。你能告訴我在哪裏添加它嗎? –

+1

'<?php set_time_limit(0)...?>'。此外,您應該儘量避免運行「緊密」的循環,這些循環不會讓其他進程有機會獲得一些CPU時間。在for循環結束之前引入'sleep(50)'或'sleep(100)'。 –

回答

0

在你的代碼的頂部,加入這一行:

set_time_limit(0); 

所以你的整個代碼看起來是這樣的:瞭解更多關於set_time_limit()here

<?php 
set_time_limit(0); 
$i=0; 
for(;;) 
{ 
$subdata=file_get_contents("/home/openflow/subscribedata.txt"); 
$subdata2=file_get_contents("/home/openflow/subscribedatatemp.txt"); 

if($subdata!=$subdata2) 
{ 
copy("/home/openflow/subscribedatatemp.txt","/home/openflow/subscribedata.txt"); 
$sub=file_get_contents("/home/openflow/subscribedata.txt"); 
$i++; 
echo "\n". $i."--".$sub; 
} 
} 
?> 

此外,請檢查您的php.ini文件中的max_execution_time未設置。

+0

我試過同樣的事情,數據不會被回顯,直到執行結束..它不應該是這樣的嗎? 每當有一個錯誤,應該有一個回聲.. @imulsion –

+1

確保你已經禁用輸出緩衝(ini設置'output_buffering') - 更多http://php.net/manual/en/book。 outcontrol.php – Tharsan

0

使用set_time_limit()可通過在腳本的開始處傳入0以防止腳本超時,從而使其無限等待。

你也想添加一個sleep()到你的循環中,這樣它就不會消耗你所有的CPU週期。

最後,您需要在每個循環的末尾添加一個flush()以清空緩衝區並將其發送到客戶端。

<?php 
// set an infinite timeout 
set_time_limit(0); 
// create an infinite loop 
while (true) { 
    // do your logic here 
    $subdata=file_get_contents("/home/openflow/subscribedata.txt"); 
    $subdata2=file_get_contents("/home/openflow/subscribedatatemp.txt"); 
    // .... etc. 

    // flush buffer to the client 
    flush(); 

    // go to sleep to give the CPU a break 
    sleep(100); 
} 
?> 
0

如果我正確認識你,你想每次循環(而不是等待整個腳本結束)後顯示輸出,讓迴音(flush)每個循環,你需要在循環結束時添加flush()

此外,我們將在循環之前添加set_time_limit(0),因此我們的腳本需要時間來執行。

<?php 
set_time_limit(0); 
ob_end_flush(); 


$i=0; 
for(;;) 
{ 
$subdata=file_get_contents("/home/openflow/subscribedata.txt"); 
$subdata2=file_get_contents("/home/openflow/subscribedatatemp.txt"); 

if($subdata!=$subdata2) 
{ 
copy("/home/openflow/subscribedatatemp.txt","/home/openflow/subscribedata.txt"); 
$sub=file_get_contents("/home/openflow/subscribedata.txt"); 
$i++; 
echo "\n". $i."--".$sub; 
/*flush()*/ 
} 

} 
ob_start(); 
?> 
+0

nope ..這甚至沒有顯示任何類型的數據..添加刷新沒有得到我的任何數據..如果我刪除時間限制和運行腳本太...我以前得到的輸出是顯示..剛剛得到一個空白屏幕 –

+0

@KiranVemuri我編輯我的代碼。看看它是否有效。 – Abdulaziz

+0

我在一段時間後再次獲得相同的空白屏幕:| –

0

使用flush,並在你的循環結束sleep功能...

但它會更好地使用AJAX調用這個..