开发者

How can I read 30MB text file using php script?

Hi I do have a text file with size of upto 30MB I would like 开发者_运维知识库to read this file using PHP loop script

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

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

Is there any way? I want open it for reading php doesnt allow me to open a 30MB file.


You could read it line by line like this:

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


Read line by line using:

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

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


If it is suitable for you to read the file piece-by-piece you can try something like this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜