Created
September 6, 2013 08:52
-
-
Save jcambien/6461251 to your computer and use it in GitHub Desktop.
Count big files lines number
https://gist.github.com/tyx/6461136
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = new SplFileObject("mybigfile.csv"); | |
// Store flags and position | |
$flags = $file->getFlags(); | |
$current = $file->key(); | |
// Prepare count by resetting flags as READ_CSV for example make the tricks very slow | |
$file->setFlags(null); | |
// Go to the larger INT we can as seek will not throw exception, errors, notice if we go beyond the bottom line | |
$file->seek(PHP_INT_MAX); | |
// We store the key position | |
// As key starts at 0, we add 1 | |
$count = $file->key() + 1; | |
// We move to old position | |
// As seek method is longer with line number < to the max line number, it is better to count at the beginning of iteration | |
$file->seek($current); | |
// Re set flags | |
$file->setFlags($flags); | |
echo $count.PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment