#40183 [Opn->Bgs]: fopen() for appending does not move position until first I/O
| From: | iliaa@php.net | Date: | Sun, 21 Jan 2007 19:01:13 +0000 |
| Subject: | #40183 [Opn->Bgs]: fopen() for appending does not move position until first I/O | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-107839@lists.php.net to get a copy of this message | ||
ID: 40183
Updated by: iliaa@php.net
Reported By: perrog at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Filesystem function related
Operating System: Mac OS X 10.4
PHP Version: 5.2.1RC3
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
The initial position returned is 0 because counting starts
from the append position (earlier data does not count).
Previous Comments:
------------------------------------------------------------------------
[2007-01-21 07:26:48] perrog at gmail dot com
Description:
------------
Opening a file for appending does not move the file pointer to
the end of file before the first I/O operation is performed.
That makes ftell() return zero directly after the fopen()
call.
The workaround, if you need to call ftell() immediately after
the fopen(), is to use a no-operation I/O call fseek() that
don't do anything. Thus, the file pointer is updated and
returns correct position information.
Reproduce code:
---------------
// create or open a file, and write some stuff to it.
$filename = "/path/to/file.txt";
$fp = fopen($filename, "a+");
fwrite($fp, "Sample text");
fclose($fp);
// re-open it and check what ftell() returns
$fp = fopen($filename, "a+");
echo " pre-fseek: " . ftell($fp) . "\n"; // ftell returns 0
fseek($fp, 0, SEEK_END);
echo "post-fseek: " . ftell($fp) . "\n"; // ftell now ok
Expected result:
----------------
The snippet should print the file size of the file:
pre-fseek: 11
post-fseek: 11
Actual result:
--------------
The snippet should print the correct file size only after the
first I/O operation:
pre-fseek: 0
post-fseek: 11
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40183&edit=1