append to a file in php

To append content to an existing file in PHP, you can use the built-in fwrite() function with the FILE_APPEND flag set. Here is an example:

main.php
$filename = "path/to/file.txt";

// Open the file in append mode
$file_handle = fopen($filename, "a");

// Write the content to the file
$content = "Some text to append";
fwrite($file_handle, $content);

// Close the file handle
fclose($file_handle);
251 chars
12 lines

In the code above, we first obtain a file handle by opening the file in append mode using the fopen() function. We then use the fwrite() function to write the content to the file. The FILE_APPEND flag ensures that the content is appended to the existing file instead of overwriting it. Finally, we close the file handle using the fclose() function.

Note that if the file doesn't exist already, it will be created automatically in append mode.

gistlibby LogSnag