Search code examples
phphtmlfile-put-contents

How can I insert text into another file [file_put_contents]


I've been trying to use php's file management systems, but I can't figure out how to do one thing. I have a file that I am going to use as a template file. Basically I would copy the contents of the template file and save them as another file.

I tried to use file_put_contents to insert a string into the copied file, but rather than simply inserting the string somewhere in the copied file, it completely replaces the entire code with the string.

How could I put a string into a php file without erasing the already existing code?


Solution

  • Looks like you are not specifying the FILE_APPEND flag when calling file_put_contents.

    Example:

    file_put_contents($file, $person, FILE_APPEND);
    

    You can find the documentation here:

    https://www.php.net/manual/en/function.file-put-contents.php