0

i would like to store a string into a .php file,but as such , its a php script itself , in which i would like to add array(s),but so far i always append string like this

<?php
$.....
....
?>

but i would like to only add the content without opening and closing new block of code. so to have only one opening and closing of script , thus appending before the actual end of file. How can i efficiently move the pointer like this ?

my function so far looks like this

 $fw = fopen('../Resource/articlelist.php',"a");
 .....
 fwrite ($fw,$diffWrite);

1 Answer 1

2

The PHP interpreter doesn't require a closing ?> on the end of files, just omit that and you it will work the same.


(just to answer the title of the question)
For moving the internal pointer of the file resource, there's fseek().

$fd = fopen('file', 'a');
fseek($fd, -3, SEEK_END); // now the pointer is at the end -3 bytes

You can create a loop that reads in chunks from the end moving backwards and search for the first ?> if you want to make this appending more robust.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.