note 93270 added to function.fopen

From: Date: Mon, 31 Aug 2009 21:07:50 +0000
Subject: note 93270 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-160094@lists.php.net to get a copy of this message
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"; if (fwrite($handle, $writestring) === false) { echo "Cannot write to text file. <br />"; } fclose($handle); ?> ---- Server IP: 66.207.199.35 Probable Submitter: 99.230.240.8 ---- Manual Page -- http://www.php.net/manual/en/function.fopen.php Edit -- https://master.php.net/note/edit/93270 Del: integrated -- https://master.php.net/note/delete/93270/integrated Del: useless -- https://master.php.net/note/delete/93270/useless Del: bad code -- https://master.php.net/note/delete/93270/bad+code Del: spam -- https://master.php.net/note/delete/93270/spam Del: non-english -- https://master.php.net/note/delete/93270/non-english Del: in docs -- https://master.php.net/note/delete/93270/in+docs Del: other reasons-- https://master.php.net/note/delete/93270 Reject -- https://master.php.net/note/reject/93270 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160094) next »