php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40183 fopen() for appending does not move position until first I/O
Submitted: 2007-01-21 07:26 UTC Modified: 2007-01-22 08:59 UTC
From: perrog at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.1RC3 OS: Mac OS X 10.4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: perrog at gmail dot com
New email:
PHP Version: OS:

 

 [2007-01-21 07:26 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-21 19:01 UTC] iliaa@php.net
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).
 [2007-01-22 08:59 UTC] perrog at gmail dot com
Yes, you have right. I became aware that even the ISO C 
Library Standard mentions ftell() immediately after fopen() 
returns a value that is implementation-defined (17.19.3):
 
 "If a file can support positioning requests (such as a disk 
 file, as opposed to a terminal), then a file position 
 indicator associated with the stream is positioned at the 
 start (character number zero) of the file, unless the file 
 is opened with append mode in which case it is
 implementation-defined whether the file position indicator 
 is initially positioned at the beginning or the end of the 
 file."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 05 17:01:31 2024 UTC