2011-01-31 11 views
1

こんにちは、私はPHPのループスクリプトPHPスクリプトを使用して30MBのテキストファイルを読むにはどうすればよいですか?

$lines = file('data.txt'); 

//loop through each line 
foreach ($lines as $line) { \\some function } 

を使用してこのファイルを読みたい点で最大30メガバイトのサイズを持つテキストファイルがどのような方法がありますがありますか?私は30MBのファイルを開くことができませんPHPを読んでそれを開きたい。

+2

あなたはファイルをどうしますか? –

+0

テキストファイルの情報を使用してデータを処理し、MySQlデータベースに挿入します。 – leon

答えて

6

あなたは次のように行ずつ、それを読むことができる:

$file = fopen("data.txt", "r") or exit("Unable to open file!"); 

while(!feof($file)) { 
    // do what you need to do with it - just echoing it out for this example 
    echo fgets($file). "<br />"; 
} 

fclose($file); 
+0

PHPが30MBのファイルを開くことができません。 – leon

+0

@leonこのコードを試しましたか? PHPは何を言ったのですか? –

+0

私が投稿したことをしようとすると、何と言われますか? – xil3

0

読み取りラインをラインで使用して:

 
$handle = fopen ("data.txt", "r"); 

while (($buffer = fgets ($handle, 4096)) !== false) { 
    // your function on line; 
} 
+1

なぜ警告を抑制していますか? (@) – xil3

+0

ひどいうさぎのごめん - 削除 – bensiu

0

それが適している場合は、少しずつあなたのファイルを読み取るためにこのようなものを試すことができます

$fd = fopen("fileName", "r"); 
while (!feof($fd)) { 
$buffer = fread($fd, 16384); //You can change the size of the buffer according to the memory you can youse 
//Process here the buffer, piece by piece 
} 
fclose($fd); 
関連する問題