Realization of php to Read Super Large File

  • 2021-07-10 18:59:54
  • OfStack

Generally speaking, when php reads large files, the method we adopt is to fetch them one line at a time, instead of writing all files into memory at a time, which will cause php program to get stuck. Here is an example.

Read the last few lines of data in a large file:


<?php
/**
 *  Fetch the last of the file $n Row 
 * 
 * @param string $filename  File path 
 * @param int $n  The last few lines 
 * @return mixed false Indicates an error, and returns a string on success 
 */
function FileLastLines($filename, $n){
   if(!$fp = fopen($filename, 'r')){
    echo " Failed to open the file, please check that the file path is correct, and the path and file name should 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 In fact, in fact, the ;
        }else{
        break;
        }
      }
    $str .= fgets($fp);
    $eof = "";
    $n In fact, in fact, the ;
    }
  return $str;
  }

echo nl2br(FileLastLines('sss.txt', 4));
/**
 * * *  Fetch the last of the file $n Row  *
 * 
 * @param string $filename  File path  *
 * @param int $n  The last few lines  *
 * @return mixed false Indicates an error, and returns a string on success 
 */
function FileLastLines($filename, $n){
  if(!$fp = fopen($filename, 'r')){
    echo " Failed to open the file, please check that the file path is correct, and the path and file name should 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));

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

Readers can improve and perfect it according to the characteristics of this example, so as to make it more in line with their own application needs.


Related articles: