note 30503 modified in function.file by danbrown

From: Date: Sun, 01 Mar 2009 18:41:34 +0000
Subject: note 30503 modified in function.file by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151297@lists.php.net to get a copy of this message
Note: Now that file() is binary safe it is 'much' slower than it used to be. If you are planning to read large files it may be worth your while using fgets() instead of file() For example: <?php $fd = fopen ("log_file.txt", "r"); while (!feof ($fd)) { $buffer = fgets($fd, 4096); $lines[] = $buffer; } fclose ($fd); ?> The resulting array is $lines. I did a test on a 200,000 line file. It took seconds with fgets() compared to minutes with file(). --was-- Note: Now that file() is binary safe it is 'much' slower than it used to be. If you are planning to read large files it may be worth your while using fgets() instead of file() For example: $fd = fopen ("log_file.txt", "r"); while (!feof ($fd)) { $buffer = fgets($fd, 4096); $lines[] = $buffer; } fclose ($fd); The resulting array is $lines. I did a test on a 200,000 line file. It took seconds with fgets() compared to minutes with file(). http://php.net/manual/en/function.file.php

« previous php.notes (#151297) next »