Well , it depends on the size of the file. If the file will fit into memory easily, the easiest way it:
- Open the file with mode r+
- Read in the entire contents of the file (e.g. stream_get_contents)
- Modify the contents in memory in whatever way you like
- using ftruncate and fseek, truncate the file, seek back to the beginning and rewrite the entire contents
- Close the file
Some locking is probably needed to do this safely - if necessary, obtain an exclusive lock after opening (This is why you need to use the same file handle throughout the operation and keep it open even while the file's in memory).
For larger files, it's going to be more complicated - your best bet is to probably read the old file into a temporary file, making mods as necessary, then after you've finished, rename the temp file over the old one. But the locking for this approach is more tricky.
Mark
Mark