Search code examples
phppointersappendfseek

PHP appending to file from specific position


In php i am opening a text file and appending to it. However I need to append 3 chars before the end of file. In other words i need to append/write from a specific place in the file.

Can any one help?

Best Regards Luben


Solution

  • You need to open the file for edit, seek to the desired position and then write to the file, eg.:

    <?php
      $file = fopen($filename, "c");
      fseek($file, -3, SEEK_END);
      fwrite($file, "whatever you want to write");
      fclose($file);
    ?>
    

    Further reference at php.net - fseek doc

    Hope that helps.