note 81325 added to function.fopen
| From: | jphansen at uga dot edu | Date: | Fri, 22 Feb 2008 15:04:17 +0000 |
| Subject: | note 81325 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136403@lists.php.net to get a copy of this message | ||
If you open a file with r+ and execute an fwrite(), writing less to the file than what it originally
was, it will result in the difference being padded with the end of the file from the previous end of
the file. Example:
<?
// Open file for read and string modification
$file = "/test";
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
$new_contents = str_replace("hello world", "hello", $contents);
fclose($fh);
// Open file to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_contents);
fclose($fh);
?>
If the end of the file was "abcdefghij", you will notice that the difference in
"hello world" and "hello", 6 characters, will be appended to the file, resulting
in the new ending: "efghij". To obviate this, fopen() with +w instead, which truncates the
file to zero length.
----
Server IP: 69.147.83.197
Probable Submitter: 128.192.32.117
----
Manual Page -- http://www.php.net/manual/en/function.fopen.php
Edit -- https://master.php.net/note/edit/81325
Del: integrated -- https://master.php.net/note/delete/81325/integrated
Del: useless -- https://master.php.net/note/delete/81325/useless
Del: bad code -- https://master.php.net/note/delete/81325/bad+code
Del: spam -- https://master.php.net/note/delete/81325/spam
Del: non-english -- https://master.php.net/note/delete/81325/non-english
Del: in docs -- https://master.php.net/note/delete/81325/in+docs
Del: other reasons-- https://master.php.net/note/delete/81325
Reject -- https://master.php.net/note/reject/81325
Search -- https://master.php.net/manage/user-notes.php