2010-07-26 15 views
0

我有一個相當大的.txt文件(〜220Mb),我需要通過100行(\ n符號)塊(例如)讀取它。我怎樣才能使用PHP?如何通過100行讀取大文件?

謝謝。

+0

可能重複的[如何在PHP中打開一個文件從X行到Y行?](http://stackoverflow.com/questions/514673/how-do-i-open-a-file - 從線-X - 線-Y-在-PHP) – Gordon 2010-07-26 14:47:38

回答

1

fopenfgetsfgets manual page有一個逐行讀取文件的例子,但不會一次加載到內存中。

0
$fp = open('big_text_file.txt',"r"); 
if($fp){ 
    $c = 0; 
    $data = array(); 
    while(!feof($fp)){ 
     if($c == 100){ 
      $c = 0; 
      // Do whatever it is you want here 
      unset($data); 
      $data = array(); 
     } 
     $data[] = fgets($fp,4096);   
     $c++; 
    } 
    if($c > 0){ 
     // Do whatever you need to again 
    } 
    fclose($fp); 
} 
相關問題