General Read files we use fopen or file_get_contents, the former can be read circularly, the latter can be read at once, but the contents of the file are loaded one at a single operation.
If the loaded file is particularly large, such as hundreds of M, on G, then performance down, then PHP has no large file processing functions or classes? The answer is: yes. PHP is really more and more "object-oriented", some of the original basis of the SPL methods are beginning to realize class. Starting with the PHP 5.1.0, the SPL library adds Splfileobject and splfileinfo two standard file operation classes. Splfileinfo is implemented from the PHP 5.1.2. In the literal sense, it can be seen that splfileobject is more powerful than splfileinfo. Yes, Splfileinfo is used only to get some property information about a file, such as file size, file access time, file modification time, suffix name equivalence, and Splfileobject is inherited splfileinfo these features. Code as follows:/** returns the contents of the file from line x to Y (support PHP5, PHP4) * @param string $filename filename * @param int $startLine Number of rows * @param int $endLine * @return string */function Getfilelines ($filename, $startLine = 1, $ endline=50, $method = ' rb ') { $content = array (); $count = $endLine-$startLine; //Judge PHP version (because you want to use splfileobject,php>=5.1.0) if (Version_compare (php_version, ' 5.1.0 ', ' >= ')) { $fp = new Splfileobject ($filename, $method); $FP->seek ($startLine-1);//go to Nth Line, seek method parameters from 0 start count for ($i = 0; $i <= $count + + $i) { $content []= $fp->current (); Current () Get contents $FP->next ()//Next line } & nbsp }else{//php<5.1 $fp = fopen ($filename, $method); if (! $fp) retur n ' error:can not read file '; for ($i =1 $i < $startLine + + $i) {//Skip front $startline line Fgets ($FP); } for ($i; $i <= $endLine; + $i) { $content []=fgets ($FP)//Read file line contents } fclose ($FP); } return Array_filter ($content); Array_filter filter: False,null, '} Ps: None of the "read to the end of the judgment":! $fp->eof () or!feof ($FP), plus this judgment affects efficiency, You can also test many, many, many lines of running time, and there is absolutely no need to add them. From the above function you can see that using SplfileobjeThe CT scan is much faster than the fgets below, especially when the number of rows is very high and the contents are to be followed. Fgets to two cycles to be able to, and to cycle $endline times. This method took a lot of effort, testing a lot of writing, is to get the most efficient method. Who feel that there is merit to improve the welcome enlighten. Use, returns 35270 rows-35280 lines of content: Code as follows: Echo ' <pre> '; Var_dump (Getfilelines (' test.php ', 35270,35280)); Echo ' </pre> ';