note 93272 added to function.fopen

From: Date: Mon, 31 Aug 2009 23:27:06 +0000
Subject: note 93272 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-160096@lists.php.net to get a copy of this message
The correction to my last post is that the old data needs to be added to the end of the new data.. Corrected below... 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); ?> ---- Server IP: 216.235.13.26 Probable Submitter: 99.230.240.8 ---- Manual Page -- http://www.php.net/manual/en/function.fopen.php Edit -- https://master.php.net/note/edit/93272 Del: integrated -- https://master.php.net/note/delete/93272/integrated Del: useless -- https://master.php.net/note/delete/93272/useless Del: bad code -- https://master.php.net/note/delete/93272/bad+code Del: spam -- https://master.php.net/note/delete/93272/spam Del: non-english -- https://master.php.net/note/delete/93272/non-english Del: in docs -- https://master.php.net/note/delete/93272/in+docs Del: other reasons-- https://master.php.net/note/delete/93272 Reject -- https://master.php.net/note/reject/93272 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160096) next »