PHP implementation reads a 1G file size

Source: Internet
Author: User

The requirements are as follows: an existing log file of about 1G, about more than 5 million lines, with PHP to return the last few lines of content.

1. Use the file function directly to manipulate or file_get_content () positive memory overflow
Note: 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 limit is limited to 16M of memory by default, which is through php.ini Memory_ Limit = 16M To set, this value if set-1, the memory makes the usage unrestricted.

The following is a piece of code that uses file to remove the last line of the document.

Ini_set (' Memory_limit ', '-1 '); $file = ' Access.log '; $data file ($file); $line $data [Count($data)-1];

Here is a problem, if the server's memory is only 256MB, then the above method is not, the solution is to manipulate the file pointer, such as the following code

2. Use PHP fseek directly for file operation

This method is the most common way, it does not need to read all the contents of the file into the memory, but directly through the pointer to operate, so efficiency is quite efficient. There are many different ways to use fseek to manipulate files, and the efficiency may also vary slightly, and here are two common approaches.

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.

functionTail$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);

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.

The implementation code is as follows

$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--; }    $t= " "; $data.=fgets($fp); $line--;}fclose($fp);Echo $data

PHP implementation reads a 1G file size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.