Editing a remote file on-the-fly with PHP

Posted by user275074 on Stack Overflow See other posts from Stack Overflow or by user275074
Published on 2010-04-23T08:38:22Z Indexed on 2010/04/23 8:43 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

Hi,

I have a requirement to edit a remote text file on-the-fly, the content of which currently stands at ~1Mb.

I have tried a couple of approaches and both seem to be clunky or hog memory which I can't rely on.

Thinking out logically what I'm trying to achieve is:

  1. FTP to a remote server.
  2. Download a copy of the file for backup purposes and store it somewhere locally.
  3. Open the remote file and add the necessary lines required.
  4. Remove lines from the remote file as per an array of un-required data generated from the local server.

Is this possible?

I've managed to code steps 1 and 2 but I'm having difficult with 3 and 4. The way I'm doing it now is to use fgets and return the whole string. Really, I don't want to do this as it involves manipulating and re-generating the whole string (and it's large) and then re-inserting it in between two markers in the remote file.

Is there no way of manipulating the lines of text in the file on-the-fly?

© Stack Overflow or respective owner

Related posts about fopen

Related posts about fgets