2016-01-11 25 views
0

你好,我需要閱讀5GB的大文件,我得到這樣的錯誤,致命錯誤:內存(分配的786432個)(試圖分配5805810472個字節)在PHP 5GB文件中讀取大文件

<?php 
set_time_limit(0); 
ini_set('memory_limit','-1'); 

$tel = file_get_contents('result.txt'); 
preg_match_all('/\"telScheme\":\"(.*?)\"/s',$tel,$tels); 

$out = fopen("phone.txt","a"); 
foreach($tels[0] as $fin){ 
$fin = str_replace('"', '', $fin); 
fwrite($out,"$fin\n"); 
} 
fclose($out); 

?> 
+2

你告訴PHP讀取大型文件5GB到內存中,並得到一個「內存不足」的錯誤.....你是怎麼想到....嘗試閱讀一次行,所以你一次只能在內存中有一行數據 –

+0

我需要一個副本到我的代碼 – Brucea

+3

所以你想要我爲你寫代碼?沒有!但我會指出你[fopen()](http://www.php.net/manual/en/function.fopen.php),[fgets()](http://www.php.net/ manual/en/function.fgets.php)和[fclose()](http://www.php.net/manual/en/function.fclose.php) –

回答

0

正確的方式TOPIC END

<?php 
set_time_limit(0); 
ini_set('memory_limit','-1'); 


$handle = @fopen("result.txt", "r"); 
$out = fopen("phone.txt","a"); 

if ($handle) { 
    while (($buffer = fgets($handle, 4096)) !== false) { 
    preg_match_all('/\"telScheme\":\"(.*?)\"/s',$buffer,$tels); 

    foreach($tels[0] as $fin){ 
    $fin = str_replace('"', '', $fin); 
    fwrite($out,"$fin\n"); 
    } 

    } 
    fclose($handle); 
} 

fclose($out); 

?>