Home:ALL Converter>fseek(): stream does not support seeking

fseek(): stream does not support seeking

Ask Time:2020-01-24T14:31:17         Author:Raghul SK

Json Formatter

Read the .txt file and get the last line of the text file, the function that I used is commented below

function read_last_line (){

      $line = '';
      
      $f = fopen('localpath\data.txt', 'r');
      $cursor = -1;
      
      fseek($f, $cursor, SEEK_END);
      $char = fgetc($f);
      
      /**
       * Trim trailing newline chars of the file
       */
      while ($char === "\n" || $char === "\r") {
          fseek($f, $cursor--, SEEK_END);
          $char = fgetc($f);
      }
      
      /**
       * Read until the start of the file or first newline char
       */
      while ($char !== false && $char !== "\n" && $char !== "\r") {
          /**
           * Prepend the new char
           */
          $line = $char . $line;
          fseek($f, $cursor--, SEEK_END);
          $char = fgetc($f);
      }      
      echo $line;
      }

While Executing the above code, I get the fseek(): stream does not support seeking, Need a solution to resolve the issue.

Author:Raghul SK,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59891417/fseek-stream-does-not-support-seeking
yy