Hi,
How can I start writing to a file from a certain point in that file? I don't want to start at the beginning or end. It's somewhere in the middle. How can I do this? Thanks.
Writing to file by PHP
How could I make it insert it and not overwrite it? I need the php script to insert some html when the user submits something, it needs to show up on the page permanently, how can i do this?
That won't work Php Manual him say:
Note: If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.
Looks like you'll have to open the file in r+ mode, seek and copy the end of the file to a temporary file or memory, write your data to the file, then the copied data after that. Be nice if there is some really easy insert stuff available.
Oops... guess I never noticed that tidbit in the manual.
In that case, you could use [man]file_get_contents/man to store the file's contents in a variable, [man]substr_replace/man (with a length of 0) to insert the given data at a given offset, and then [man]file_put_contents/man to write the data back into the file.
Can you give me an example because I'm a bit confused thanks
Ok - put this in a file and refresh it a few times. I haven't made an insert function out of it, but you should be able to do that easily enough.
<?php
// open this file for reading and writing
$fp = fopen(__FILE__, 'r+');
// seek past the <?php
fseek($fp, 5, SEEK_SET);
// copy the rest of the file (lazy way)
$temp = fread($fp, 1000000);
// after <?php again
fseek($fp, 5, SEEK_SET);
// stupid comment insert
fwrite($fp, "\n // Hadoken! Bless you, it's ".date('r') . $temp);
// shut that door
fclose($fp);
// let's look at this file after the insert.
highlight_file(__FILE__);
?>
EDIT: You might have a problem with file permissions if you're running on a 'real' server. If so the change the permission of the file or the directory it's in to 0777.
Rather than counting to the correct position in the page to insert new content, could you search for a string then make it insert new text after the string its found in the document? Because if it's dynamically changing the char count will change as the content of the page increases.
Thanks.
javawizkid wrote:could you search for a string
You'd have to read the file to find the string you're searching for (use a mode of "r+" so that you can write as well as read). This can get hairy because if you read the file in chunks (instead of the entire thing in one big hit) there's the chance that the string you're looking for will be broken across chunks. Once you've found it you can use ftell() to record its position in the file (you'll need it again later). This may require a bit of arithmetic because by the time you've found the string you may have moved a bit past it depending on where it turned out to be in the last chunk(s) you read.
After that you have to read the entire remainder of the file so that it doesn't get overwritten by your additional text.
Then you can use the file position you got earlier to fseek() back to the search string, write your new text, and then write everything else that follows it.