Home > Backend Development > PHP Tutorial > Two ways to implement the php fseek function to read large files

Two ways to implement the php fseek function to read large files

墨辰丷
Release: 2023-03-28 20:54:02
Original
1651 people have browsed it

Reading files in php is very simple, but if the file read is very large, how to solve it? We can directly use fseek to perform large file operations. This article introduces how PHP uses the fseek function to read large files. Friends who need it can refer to

php to read large files. Using the fseek function is the most common. method, it does not need to read all the contents of the file into the memory, but operates directly through pointers, so the efficiency is quite efficient. When using fseek to operate files, there are many different methods, and the efficiency may be slightly There are differences. The following are two commonly used methods.

Method 1:

First find the last EOF of the file through fseek, and then find the beginning of the last line The starting position, get the data of this row, then find the starting position of the next row, then take the position of this row, and so on, until the $num row is found. The implementation code is as follows:

The entire code execution takes 0.0095 (s)

function tail($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));
Copy after login

Method 2:

Still use fseek to read from the end of the file, but this time it is not reading bit by bit, but reading piece by piece. Every time a piece of data is read, the read data is placed in a buf , and then use the number of newline characters (n) to determine whether the last $num rows of data have been read. The implementation code is as follows

The entire code execution takes 0.0009(s).

$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
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP uses the preg_split function to split strings containing newlines and semicolons

PHP writing program is implemented as follows Function

array_keys in function

##array_keys returns the key name of the array

##### #######

The above is the detailed content of Two ways to implement the php fseek function to read large files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template