leon
leon

Reputation: 151

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.

Upvotes: 1

Views: 540

Answers (3)

Danilo
Danilo

Reputation: 2686

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

Upvotes: 0

bensiu
bensiu

Reputation: 25564

Read line by line using:

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

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

Upvotes: 0

xil3
xil3

Reputation: 16439

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

Upvotes: 6

Related Questions