1

I have few text files with size more than 30MB.

How can i read such giant text files from PHP?

codaddict
  • 445,704
  • 82
  • 492
  • 529
Pradip
  • 1,317
  • 4
  • 21
  • 36

2 Answers2

6

Unless you need to work with all the data at the same moment, you can read them in pieces. Example for binary files:

<?php
$handle = fopen("/foo/bar/somefile", "rb");
$contents = '';
while (!feof($handle)) {
  $block = fread($handle, 8192);
  do_something_with_block($block);
}
fclose($handle);
?>

The above example might break multibyte encodings (in case there's a multibyte character across the 8192-byte boundary - e.g. Ǿ in UTF-8), so for files that have meaningful endlines (e.g. text), try this:

<?php
$handle = fopen("/foo/bar/somefile", "rb");
$contents = '';
while (!feof($handle)) {
  $line = fgets($handle);
  do_something_with_line($line);
}
fclose($handle);
?>
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 1
    As far as I know, `fgets()` will still mess it up if the file's encoding does not have ASCII-compatible, one-byte line endings, e.g. UTF-16. – scy Nov 07 '14 at 15:28
3

You can open the file using fopen, read the lines using fgets.

$fh = fopen("file", "r");  // open file to read.

while (!feof($fh)) { // loop till lines are left in the input file.
        $buffer = fgets($fh); //  read input file line by line.
        .....
        }       
}       

fclose($fh);
codaddict
  • 445,704
  • 82
  • 492
  • 529