note 93272 deleted from function.fopen by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:16:33 +0000 |
| Subject: | note 93272 deleted from function.fopen by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197061@lists.php.net to get a copy of this message | ||
Note Submitter: Mikhail Nemtsev
----
As someone has mentioned already, using fopen() in r+ mode overwrites the beginning of the file with
new data. So, if you want to append new data to the beginning of the file, do so as follows:
<?php
$filename = "myfile.txt";
//first, obtain the data initially present in the text file
$ini_handle = fopen($filename, "r");
$ini_contents = fread($ini_handle, filesize($filename));
fclose($ini_handle);
//done obtaining initially present data
//write new data to the file, along with the old data
$handle = fopen($filename, "w+");
$writestring = "text to write to file\n" . $ini_contents;
if (fwrite($handle, $writestring) === false) {
echo "Cannot write to text file. <br />";
}
fclose($handle);
?>