How can one append new content to an existing file without overwriting the existing content in PHP?
To append new content to an existing file without overwriting the existing content in PHP, you can use the `FILE_APPEND` flag with the `file_put_contents()` function. This flag tells PHP to append the new content to the end of the file instead of overwriting it. You can also use the `fopen()`, `fwrite()`, and `fclose()` functions to achieve the same result.
// Using file_put_contents() function
$file = 'example.txt';
$newContent = "This is the new content to append.";
file_put_contents($file, $newContent, FILE_APPEND);
// Using fopen(), fwrite(), and fclose() functions
$file = 'example.txt';
$newContent = "This is the new content to append.";
$handle = fopen($file, 'a');
fwrite($handle, $newContent);
fclose($handle);
Keywords
Related Questions
- Can JavaScript be a better alternative to PHP for distinguishing between browser types?
- What is the significance of the error message "Parse error: syntax error, unexpected T_FUNCTION" in PHP code?
- In what situations would it be necessary to use the PEAR-Installer to add a package like Text_Template to PHPUnit in PHP?