php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42066 file pointer not changed when file truncated with append mode
Submitted: 2007-07-22 12:04 UTC Modified: 2007-09-02 21:42 UTC
From: kraghuba at in dot ibm dot com Assigned: bjori (profile)
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2007-07-22 (snap) OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kraghuba at in dot ibm dot com
New email:
PHP Version: OS:

 

 [2007-07-22 12:04 UTC] kraghuba at in dot ibm dot com
Description:
------------
The file pointer position is not changed when a file opened in append mode is truncated using ftruncate() function. As per the documentation of ftruncate() function, the file pointer should be changed.

http://in2.php.net/manual/en/function.ftruncate.php
from doumentation: 
...
Note:  The file pointer is changed only in append mode. In write mode, additional fseek()  call is needed.
...

This is applicable to php5 as well as php6. 
checked it on WinXP and RHEL 5.

Reproduce code:
---------------
<?php
  $fp = fopen("test.txt", "w");
  fwrite($fp, "testing ftrucate function with append mode");
  fclose($fp);
  var_dump( filesize("test.txt") );

  $fp = fopen ("test.txt", "a");
  var_dump( ftell($fp) );
  var_dump( ftruncate($fp, 10) );
  var_dump( ftell($fp) );
  fclose($fp);

  clearstatcache();
  var_dump( filesize("test.txt") );
?>


Expected result:
----------------
int(42)
int(0)
bool(true)
int(10)
int(10)



Actual result:
--------------
int(42)
int(0)
bool(true)
int(0)
int(10)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-02 21:42 UTC] bjori@php.net
From man ftruncate(2): "The file offset is not changed."

I've fixed the docs.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 21 21:00:03 2025 UTC