Read file lines backwards (fgets) with php

前端 未结 4 1930
轮回少年
轮回少年 2021-01-17 18:03

I have a txt file that I want to read backwards, currently I\'m using this:

$fh = fopen(\'myfile.txt\',\'r\');
while ($line = fgets($fh)) {
  echo $line.\"         


        
4条回答
  •  遇见更好的自我
    2021-01-17 18:51

    Here is my solution for just printing the file backwards. It is quite memory-friendly. And seems more readable (IMO [=in my opinion]).

    It goes through the file backwards, count the characters till start of a line or start of the file and then reads and prints that amount of characters as a line, then moves cursor back and reads another line like that...

      if( $v = @fopen("PATH_TO_YOUR_FILE", 'r') ){ //open the file
        fseek($v, 0, SEEK_END); //move cursor to the end of the file
        /* help functions: */
        //moves cursor one step back if can - returns true, if can't - returns false
        function moveOneStepBack( &$f ){ 
          if( ftell($f) > 0 ){ fseek($f, -1, SEEK_CUR); return true; }
            else return false;
        }
        //reads $length chars but moves cursor back where it was before reading 
        function readNotSeek( &$f, $length ){ 
          $r = fread($f, $length);
          fseek($f, -$length, SEEK_CUR);
          return $r;  
        }
    
        /* THE READING+PRINTING ITSELF: */
        while( ftell($v) > 0 ){ //while there is at least 1 character to read
          $newLine = false;
          $charCounter = 0;
    
          //line counting
          while( !$newLine && moveOneStepBack( $v ) ){ //not start of a line / the file
            if( readNotSeek($v, 1) == "\n" ) $newLine = true;
            $charCounter++;              
          } 
    
          //line reading / printing
          if( $charCounter>1 ){ //if there was anything on the line
            if( !$newLine ) echo "\n"; //prints missing "\n" before last *printed* line            
            echo readNotSeek( $v, $charCounter ); //prints current line  
          }   
    
        }
        fclose( $v ); //close the file, because we are well-behaved
      }
    

    Of course replace PATH_TO_YOUR_FILE with your own path to your file, @ is used when opening the file, because when the file is not found or can't be opened - warning is raised - if you want to display this warning - just remove the error surpressor '@'.

提交回复
热议问题