|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-10-29 15:21 UTC] cmb@php.net
Description: ------------ The documentation of fopen() mode c[1] states: | The file pointer is positioned on the beginning of the file. However, this does not happen with PHP 7.3 on Windows; instead the file pointer is positioned at the end of the file, even though ftell() claims otherwise. It works as expected on Linux and with PHP 7.2 on Windows. [1] <http://php.net/manual/en/function.fopen.php> Test script: --------------- <?php $filename = __DIR__ . '/fopen-c.txt'; file_put_contents($filename, 'foo'); $stream = fopen($filename, 'c'); ftruncate($stream, 0); // fseek($stream, 0); var_dump(ftell($stream)); fwrite($stream, 'bar'); fclose($stream); var_dump(file_get_contents($filename)); Expected result: ---------------- int(0) string(3) "bar" Actual result: -------------- int(0) string(6) " bar" Patchesrestore-old-position (last revision 2018-10-29 22:31 UTC by cmb@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 18:00:02 2025 UTC |
> […] if there's something wrong with fopen […] Sorry, my mistake. Actually, the problem is not with fopen() per se, but rather with ftruncate() on a file freshly opened in c mode. If I comment out the ftruncate() call in the given reproduce script, I get the expected: int(0) string(3) "bar" Also, writing only the single character 'b' instead of 'bar' gives the expected: int(0) string(3) "boo" The problem is that the ftruncate() call changes the seek pointer, which it is not supposed to.