2011-01-31 97 views
1

您好我也有高达30MB大小的文本文件,我想使用PHP脚本循环如何使用php脚本读取30MB文本文件?

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

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

有什么办法来读取这个文件?我想打开它阅读PHP不允许我打开一个30MB的文件。通过线使用

$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); 
+2

那你想用的文件呢? – 2011-01-31 15:52:01

+0

我想一行一行阅读,然后用文本文件中的信息处理数据,然后插入MySQl数据库 – leon 2011-01-31 15:56:02

回答

6

你可以逐行这样阅读

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

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

看到我的问题是php不允许我打开30mb文件。 – leon 2011-01-31 15:59:30

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);