note 42328 added to function.ftruncate

From: Date: Wed, 12 May 2004 10:35:00 +0000
Subject: note 42328 added to function.ftruncate
Groups: php.notes 
Request: Send a blank email to php-notes+get-69517@lists.php.net to get a copy of this message
I was confused by the different fopen modes (r, r+, w, w+, a, a+). I wrote <?php $alt = '1234567890'; $neu = '13579'; $datei = 'test.txt'; $dh = fopen ($datei,"w"); fwrite($dh, $alt); fclose($dh); $dh = fopen ($datei,"a+"); // just alter the mode to test others rewind($dh); echo "Inhalt<BR>\nalt: ".fread($dh, filesize($datei))."<BR>\n"; ftruncate($dh, '0'); fwrite($dh, $neu); rewind($dh); echo 'neu: '.fread($dh, filesize($datei)); fclose($dh); ?> to find out what happens: 1. For me ftruncate to a size of 0 takes the pointer to position 0 successfully. Tested under UNIX with PHP 4.2.3 and under MacOSX 10.3 with PHP 4.3.4 2. Writing to a file opened with 'a' or 'a+' always begins writing at the end of the file. A rewind before doesnt help! ('a' for always append!) macrudi ---- Manual Page -- http://www.php.net/manual/en/function.ftruncate.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42328 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42328&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42328&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69517) next »