Reading large files from end

前端 未结 10 1536
慢半拍i
慢半拍i 2020-12-28 19:47

Can I read a file in PHP from my end, for example if I want to read last 10-20 lines?

And, as I read, if the size of the file is more than 10mbs I start getting erro

相关标签:
10条回答
  • 2020-12-28 20:19

    Following snippet worked for me.

    $file = popen("tac $filename",'r');

    while ($line = fgets($file)) {

       echo $line;
    

    }

    Reference: http://laughingmeme.org/2008/02/28/reading-a-file-backwards-in-php/

    0 讨论(0)
  • Well while searching for the same thing, I can across the following and thought it might be useful to others as well so sharing it here:

    /* Read file from end line by line */

    function tail_custom($filepath, $lines = 1, $adaptive = true) {
            // Open file
            $f = @fopen($filepath, "rb");
            if ($f === false) return false;
    
            // Sets buffer size, according to the number of lines to retrieve.
            // This gives a performance boost when reading a few lines from the file.
            if (!$adaptive) $buffer = 4096;
            else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
    
            // Jump to last character
            fseek($f, -1, SEEK_END);
    
            // Read it and adjust line number if necessary
            // (Otherwise the result would be wrong if file doesn't end with a blank line)
            if (fread($f, 1) != "\n") $lines -= 1;
    
            // Start reading
            $output = '';
            $chunk = '';
    
            // While we would like more
            while (ftell($f) > 0 && $lines >= 0) {
    
                // Figure out how far back we should jump
                $seek = min(ftell($f), $buffer);
    
                // Do the jump (backwards, relative to where we are)
                fseek($f, -$seek, SEEK_CUR);
    
                // Read a chunk and prepend it to our output
                $output = ($chunk = fread($f, $seek)) . $output;
    
                // Jump back to where we started reading
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
    
                // Decrease our line counter
                $lines -= substr_count($chunk, "\n");
    
            }
    
            // While we have too many lines
            // (Because of buffer size we might have read too many)
            while ($lines++ < 0) {
                // Find first newline and remove all text before that
                $output = substr($output, strpos($output, "\n") + 1);
            }
    
            // Close file and return
            fclose($f);     
            return trim($output);
    
        }
    
    0 讨论(0)
  • 2020-12-28 20:26

    You can use fopen and fseek to navigate in file backwards from end. For example

    $fp = @fopen($file, "r");
    $pos = -2;
    while (fgetc($fp) != "\n") {
        fseek($fp, $pos, SEEK_END);
        $pos = $pos - 1;
    }
    $lastline = fgets($fp);
    
    0 讨论(0)
  • 2020-12-28 20:28

    As Einstein said every thing should be made as simple as possible but no simpler. At this point you are in need of a data structure, a LIFO data structure or simply put a stack.

    0 讨论(0)
提交回复
热议问题