Page 1 of 1

fwrite without overwriting

Posted: Sun Dec 21, 2008 1:56 pm
by mmj
How can I add text before the end and after the beginning of a file without overwriting data?

Seems like my only option is to split the file up then put it back together later on. :banghead:

Re: fwrite without overwriting

Posted: Sun Dec 21, 2008 3:08 pm
by Mark Baker

Code: Select all

 
$initialData = file_get_contents('myFile.txt');
$newData = "New First Line\n".$initialData."New Last Line\n";
file_put_contents('myFile.txt',$newData);
 

Re: fwrite without overwriting

Posted: Mon Dec 22, 2008 5:08 am
by mmj
Mark Baker wrote:

Code: Select all

 
$initialData = file_get_contents('myFile.txt');
$newData = "New First Line\n".$initialData."New Last Line\n";
file_put_contents('myFile.txt',$newData);
 
I mean the other way around. =)

Re: fwrite without overwriting

Posted: Mon Dec 22, 2008 9:52 am
by John Cartwright
So you want to overrite the contents of the file? Simply use file_get_contents() without ammending the current files content.

Re: fwrite without overwriting

Posted: Mon Dec 22, 2008 11:01 am
by mmj
Okay, if data.txt has "12567" in it then I do this:

Code: Select all

<?php
$fp = fopen('data.txt', 'r+');
fseek($fp, 2);
fwrite($fp, '34');
fclose($fp);
?>
I want "1234567" not "12347".

Basically I don't want to overwrite the data which the cursor is currently located at.

Note: this example isn't similar to what I'm doing, I'm actually modifying relatively large binary files.

Re: fwrite without overwriting

Posted: Mon Dec 22, 2008 5:29 pm
by John Cartwright
Post some examples of what you are actually doing. Your examples involve string manipulation, however you have mentioned you are modify binary contents (which is completely different).

Re: fwrite without overwriting

Posted: Tue Dec 23, 2008 9:24 am
by mmj
:banghead: :madblow:

Is there some sort of communication barrier at work here? 8O

mea culpa, I guess I should have been more descriptive in my first post.

Basically I just want to confirm what is said in this thread:

http://www.usenet-forums.com/php-langua ... iting.html

There isn't any ready-made solution to make fwrite not to overwrite.