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