SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add Data to the Middle of a File

    I'm trying to add data to a file (roughly around the middle) but when using fseek, the data I write/add simply overwrites the old data. Is there anyway I can add data to the middle of a file, without overwriting the existing data, but instead, push the existing data along to make room for the new data? My goal is to dump as much stuff to a file as i can while the script runs. Otherwise all the data will build up in the memory and php will hault execution to the memory limit. So I'm not after a solution which involves loading the file into php to make the modifications.

    Can anyone tell me how I might be able to do this?

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found it is easier to read in the contents of a file into my app, do what needs to be done (like adding content to the middle) and then over write the existing file with the new data. Trying to 'play' with a file as a file really can be a pain!

  3. #3
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm yeah. Like i said above though, that's not an option for me. I've managed to go ahead and create my script without needing to do this, but if the two data blocks in my script had to be written to the file in the other order (small block first), then I'd be screwed.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wardrop, what if you read the content of the original file (original.ext) and split it in to separate files: begin.ext and end.ext. Now your script generates the content that you write in another file: content.ext. At the end you just merge the three files. You could use the OS to merge files. In Linux you could use cat.
    Code:
    rm -f original.ext & cat begin.ext content.ext end.ext >> original.ext

  5. #5
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I generally try to avoid OS specific commands as not only will it not work on certain operating systems, but there's also the chance that the host the script runs on, won't allow php to issue commands.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •