php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41763 fopen() documentation for append mode needs more clarity
Submitted: 2007-06-21 12:54 UTC Modified: 2007-08-17 07:48 UTC
From: kraghuba at in dot ibm dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: linux, windows
Private report: No CVE-ID: None
 [2007-06-21 12:54 UTC] kraghuba at in dot ibm dot com
Description:
------------
The current documentation of fopen for append mode doesn't state clearly where the file pointer is set initally. The doc says :
"In a mode : Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.".

But in reality the file pointer is set to 0, the file position is moved to end of file before any write operation.

Please modify the document to make this clear. 

I also noticed that in response to following defect this was explained : "Bug #15528: ftell does not work consistently"


Reproduce code:
---------------
<?php
$fp = fopen(__FILE__, "a"); 
var_dump($fp);
var_dump( ftell($fp) );   // ftell should return the size of the file 
fclose($fp);
?>


Expected result:
----------------
C:\workdir\test>php fopen.php
resource(5) of type (stream)
int(0)

Actual result:
--------------
C:\workdir\test>php fopen.php
resource(5) of type (stream)
int(0)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-16 13:50 UTC] vrana@php.net
fopen(, "a") works as described but ftell() tells offset since opening:
<?php
echo filesize("x") . " ";
$fp = fopen("x", "a");
echo ftell($fp) . " ";
fwrite($fp, "a");
echo ftell($fp);
?>
outputs e.g. 5 0 1.
 [2007-08-17 07:15 UTC] kraghuba at in dot ibm dot com
The guess, the sample code given in the defect didn't make the point clear. Here is the code that would

<?php
  // create a file first
  file_put_contents("test.txt", "Testing fopen with a mode");
  

  // now open the file in append mode and check the initial
  // location of file pointer 
  $fp = fopen ("test.txt", "a");
  
  // check the position of the fp
  // expected same as return value of filesize("test.txt"), as per the doc
  // but it returns 0 which is the right behavior.
  var_dump( ftell($fp) );  
  
  fclose($fp);
?>
 [2007-08-17 07:48 UTC] vrana@php.net
The sample code is clear and my example shows you what's happening. ftell() tells you the position from the opening point. Append mode's opening point is at the end of the file so initial ftell() tells correctly 0.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Aug 14 04:00:03 2025 UTC