PHP read large files can use the file function and the fseek function, but the efficiency may be different between the two, this article introduces the PHP file function and fseek function to achieve large file reading efficiency comparison analysis, the need for friends can refer to.
1. Use the file function directly to manipulate
Because the file function reads everything into memory at once, and PHP prevents some poorly written programs from taking up too much memory and causing the server to go down, The default limit is to use only 16M of memory, which is through php.ini Memory_ Limit = 16M to set, This value if set-1, the memory makes the usage unrestricted.
Here is a piece of code that uses file to fetch the last line of the Document:
<? PHP Ini_set (' memory_limit ', '-1 '); $file = ' Access.log '; $data file ($file); $line $data [Count($data)-1]; Echo $line ; ? >
The entire code execution takes time 116.9613 (s).
My machine is 2 g of memory, when the press F5 run, the system directly gray, almost 20 minutes after the recovery, it can be seen so large files directly into memory, the consequences is how much serious, so not million can not, memory_limit this thing can not be adjusted too high, otherwise only call room, Let the Reset machine Out.
2. Use PHP fseek directly for file operation
This is the most common way, it does not need to read all the contents of the file, but directly through the pointer to operate, so efficiency is quite efficient. There are a number of different ways to use fseek to manipulate files, and the efficiency may also be slight, and here are two common methods:
Method One
first, find the last EOF of the file by fseek, then find the beginning of the last line, take this row of data, then find the starting position of the next line, then take the position of this line, and so on, until the $num line is Found.
#实现代码如下
<?PHP$fp=fopen($file, "r"); $line= 10; $pos=-2; $t= " "; $data= ""; while($line> 0) { while($t! = "\ n") { fseek($fp,$pos,seek_end); $t=fgetc($fp); $pos--; }//http://www.manongjc.com $t= " "; $data.=fgets($fp); $line--; } fclose($fp); Echo $data?>
Complete code Execution Time 0.0095 (s)
Method Two
Or use fseek way from the end of the file to read, but this is not a single read, but a piece of reading, each read a piece of data, the data will be read in a buf, and then by the number of newline characters (\ N) to determine whether the last $num line of data is Read.
#实现代码如下
<?PHP$fp=fopen($file, "r"); $num= 10; $chunk= 4096; $fs=sprintf("%u",filesize($file)); $max= (intval($fs) = = php_int_max)? Php_int_max:filesize($file); for($len= 0;$len<$max;$len+=$chunk) { $seekSize= ($max-$len>$chunk) ?$chunk:$max-$len; fseek($fp, ($len+$seekSize) *-1,seek_end); $readData=fread($fp,$seekSize) .$readData; if(Substr_count($readData, "\ N") >=$num+ 1) { //Code Farming tutorial http://www.manongjc.com Preg_match("! (. *?\n) {" . ($num) . "}$!",$readData,$match); $data=$match[0]; break; } } fclose($fp); Echo $data; ?>
The entire code execution takes time 0.0009 (s).
Method Three
<?PHPfunctionTail$fp,$n,$base= 5) { assert($n> 0); $pos=$n+ 1; $lines=Array(); while(Count($lines) <=$n) { Try { fseek($fp, -$pos,seek_end); } Catch(Exception $e) { fseek(0); break; } $pos*=$base; while(!feof($fp)) { Array_unshift($lines,fgets($fp)); } } return Array_slice($lines, 0,$n); } Var_dump(tail (fopen("access.log", "r+"), 10)); ?>
Complete code Execution Time 0.0003 (s)
Original Address: http://www.manongjc.com/article/1578.html
PHP uses the file function, fseek function to read large file efficiency analysis