Efficient method for PHP to read n rows at the end of a large file recommend _php tips

Source: Internet
Author: User

Small files within a few megabytes of size, can be through the file () function, the files are read into the array by row, in the Array_pop to get the last line, you can.

But for a large text file, the machine memory is not large enough, or PHP itself memory_limit restrictions, this method does not apply, even if the force does not limit the efficiency is very low.

There's no way? Of course, but there is no ready-made function, you need to do it yourself.

Here you need to use a file pointer, have learned C should know the pointer type a thing, popular to speak, PHP through fopen open a file, this time has not read the file, this point is the beginning of the file, the pointer position is 0, when you pass the fgets or fgetc from the file to read the content of the time , how much you read and how much the pointer goes forward, which is also

while (!feof ($fp)) {
$data. =fgets ($fp, 4096);

The principle of implementation is that fgets reads a string of the specified length backwards from the current pointer position until a newline character is met.

Is it possible to control the position of the pointer to the nth line position? Unfortunately, no, but you can move the pointer directly to the end and go backwards n positions through the fseek () function.

We first move the pointer to the end, and backward backwards 2 positions, read a character by fgetc, to determine whether this character is "\ n" is a newline character, if not a newline, then continue to reverse a position again, until we go backwards to the end of the last line of line break, Use fgets directly to remove an entire row. Here the two while loop is used, the outer loop controls the number of rows to be obtained, and the inner loop controls the fseek action.

The functions are as follows:

/**
 * Fetch file last $n line
 * @param string $filename file path
 * @param int $n last few lines
 * @return Mixed false indicates an error, and success returns a string 
   
    */
function Filelastlines ($filename, $n) {
  if (! $fp =fopen ($filename, ' R ')} {
    echo) failed to open the file, please check that the file path is correct. Path and filename do not contain Chinese ";
    return false;
  }
  $pos =-2;
  $eof = "";
  $str = "";
  while ($n >0) {
    while ($eof!= "\ n") {
      if (!fseek ($fp, $pos, Seek_end)) {
        $eof =fgetc ($fp);
        $pos-;
      } else{break
        ;
      }
    $str. =fgets ($fp);
    $eof = "";
    $n-;
  }
  return $str;
}
Echo nl2br (Filelastlines (' Sss.txt ', 4));
   

The above PHP read large file at the end of the high efficiency method recommended is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.