Php method for reading n rows at the end of a large file

Source: Internet
Author: User
This article mainly introduces PHP to read the large file at the end of the N-line method, interested in a friend's reference, I hope to be helpful to everyone.

Small files within a few megabytes in size, can be through the file () function, the files are read into the array by line, 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 is not applicable, even if the force is not limited, the efficiency is very low.

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

Here need to use the file pointer, learn c should know the pointer of what kind of thing, popular speaking, PHP open a file through fopen, this time has not read the file, this time pointing to the beginning of the file, the pointer position is 0, when you read the content from the file fgets or fgetc , how much you read, and how many pointers go forward, which is also

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


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

So can you control the position of the pointer to the last nth row position? Unfortunately, no, but you can move the pointer directly to the end and reverse the n position through the fseek () function.

We first move the pointer to the end, and backward backwards 2 positions, through the fgetc read a character, to determine whether the character is "\ n" is a newline character, if it is not a newline character, then continue to reverse a position to judge again, until we go backwards to the end of the previous line of newline character, Use the fgets directly to remove a whole line. It uses two while loops, 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 error, Success returns string */function Filelast Lines ($filename, $n) {  if (! $fp =fopen ($filename, ' R ')) {    echo] failed to open the file, check that the file path is correct, and that the path and file name 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));

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.