1

I have 9 GB MSSQL file. I want to count lines of the file. I add the following code:

$lines = file('db.sql');
echo count($lines);

I got the following error:

Fatal error: Out of memory (allocated 398458880) (tried to allocate 396361752 bytes) in C:\xampp\htdocs\import\read-line-by-line.php on line 3

chris85
  • 23,846
  • 7
  • 34
  • 51
Javed Iqbal
  • 156
  • 10
  • 3
    Clearly you can't load the entire 9GB file into memory all at once. Stop trying to do so. If you want to count lines, read the file line by line and increment a counter after each line is read. – Ken White Apr 25 '18 at 02:23
  • its require 9GB memory? – Javed Iqbal Apr 25 '18 at 02:24
  • @JavedIqbal Yes, if you open a 9 GB file you'll need at least that 9 GBs available. Read it in chunks though and you'll be fine, see http://php.net/manual/en/function.fread.php – chris85 Apr 25 '18 at 02:56
  • use iterator https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line – Ilya Kharlamov Apr 25 '18 at 02:58

1 Answers1

2

You should either increase the max_memory_limit in php.ini

or you could use the following if you are using Linux. Linux provides the most robust algorithm in counting lines.

$output = shell_exec('wc -l /dir/db.sql');
echo "<pre>$output</pre>";

Windows version

$output = shell_exec('find /v /c "" C:\filename.ext');
echo "<pre>$output</pre>";
Wils
  • 1,178
  • 8
  • 24