note 113038 added to function.fopen

From: Date: Wed, 21 Aug 2013 11:54:01 +0000
Subject: note 113038 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-196039@lists.php.net to get a copy of this message
Here is how you could modify file's contents by only opening it once in "r+" mode: <?php $filePath = '/path/to/file'; if( !@$fh = fopen($filePath, 'r+') ) { die("Could not open $filePath in READ+WRITE mode"); } // Extract contents $contents = fread($fh, filesize($filePath)); // ..modify $contents in any way you need.. // Truncate file (important to move the pointer first) fseek($fh, 0); ftruncate($fh, 0); // Write fwrite($fh, $contents); fclose($fh); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 81.218.50.34 ---- Manual Page -- http://php.net/manual/en/function.fopen.php Edit -- https://master.php.net/note/edit/113038 Del: integrated -- https://master.php.net/note/delete/113038/integrated Del: useless -- https://master.php.net/note/delete/113038/useless Del: bad code -- https://master.php.net/note/delete/113038/bad+code Del: spam -- https://master.php.net/note/delete/113038/spam Del: non-english -- https://master.php.net/note/delete/113038/non-english Del: in docs -- https://master.php.net/note/delete/113038/in+docs Del: other reasons-- https://master.php.net/note/delete/113038 Reject -- https://master.php.net/note/reject/113038 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196039) next »