Files and Streams
Append File
Appending adds new text after the existing file content.
Append File
append_file.php
<?php
$line = ;
$file = "php_append_file.txt";
file_put_contents($file, "first\n");
file_put_contents($file, $line . "\n", FILE_APPEND);
$content = trim(file_get_contents($file));
unlink($file);
echo "line=" . $line . "\n";
echo "content=" . str_replace("\n", "|", $content) . "\n";
<?php
$line = ;
$file = "php_append_file.txt";
file_put_contents($file, "first\n");
file_put_contents($file, $line . "\n", FILE_APPEND);
$content = trim(file_get_contents($file));
unlink($file);
echo "line=" . $line . "\n";
echo "content=" . str_replace("\n", "|", $content) . "\n";
<?php
$line = ;
$file = "php_append_file.txt";
file_put_contents($file, "first\n");
file_put_contents($file, $line . "\n", FILE_APPEND);
$content = trim(file_get_contents($file));
unlink($file);
echo "line=" . $line . "\n";
echo "content=" . str_replace("\n", "|", $content) . "\n";
append
`FILE_APPEND` keeps previous content and adds the next string at the end.