note 113038 deleted from function.fopen by cmb
| From: | cmb@php.net | Date: | Fri, 09 Sep 2016 22:33:33 +0000 |
| Subject: | note 113038 deleted from function.fopen by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206549@lists.php.net to get a copy of this message | ||
Note Submitter: contact at sergeylukin dot com
----
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);
?>