Seems like my only option is to split the file up then put it back together later on.
fwrite without overwriting
Moderator: General Moderators
fwrite without overwriting
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.
Seems like my only option is to split the file up then put it back together later on.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: fwrite without overwriting
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
I mean the other way around. =)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);
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: fwrite without overwriting
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
Okay, if data.txt has "12567" in it then I do this:
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.
Code: Select all
<?php
$fp = fopen('data.txt', 'r+');
fseek($fp, 2);
fwrite($fp, '34');
fclose($fp);
?>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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: fwrite without overwriting
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
Is there some sort of communication barrier at work here?
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.