php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18006 more info
Submitted: 2002-06-26 21:06 UTC Modified: 2002-07-02 16:49 UTC
From: ilia at prohost dot org Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.2.1 OS: Linux
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: ilia at prohost dot org
New email:
PHP Version: OS:

 

 [2002-06-26 21:06 UTC] ilia at prohost dot org
When doing the following in php, the result is a file that is 200 bytes, 100 bytes of NULL and 100 bytes of the actual data.

<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
ftruncate($fp, $s+100);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>

In reality there should be a 100 byte file, full of 1s. 

However, this example works
<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
ftruncate($fp, $s+100, SEEK_SET);
fseek($fp, $s, SEEK_SET);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-26 21:08 UTC] ilia at prohost dot org
Sorry, the 2nd example is wrong, here is the correct one.

<?php
$fp = fopen('a', 'a+');
$s=ftell($fp);
fseek($fp, $s+100, SEEK_SET);
fseek($fp, $s, SEEK_SET);
fwrite($fp, str_repeat('1', 100));
fclose($fp);
?>
 [2002-06-26 22:50 UTC] sniper@php.net
What happens here is that you open a file in append mode
and set the filepointer in the end of the file.
Now when you truncate it to 100 bytes, the filepointer
is still in the end. And when you add the 100 bytes, they're
added in the end. 

You should open the file in 'w' mode.

AFAIK, this is the correct behaviour.

 [2002-06-27 03:19 UTC] rasmus@php.net
No, this is not correct behaviour.  Assuming file 'a' does not exist or is of 0 length when we start, then $s should return 0 on the ftell().  ftruncate() should create a 100 byte file containing nulls *but* by definition it should not move the file pointer.  The file pointer is still at position 0.  So when the fwrite() happens the 100 nulls should be overwritten by the 100 1's.

Try this test program in C:

#include <stdio.h>

int main() {
    FILE *fp;    
    int s;

    fp = fopen("a","a+");
    s = ftell(fp);
    ftruncate(fp, s+10);
    fwrite("1111111111",1,10,fp);
    fclose(fp);
}

(didn't feel like typing out 100 1's.)

Looking through Wez's streams code I don't see any obvious bug there.
 [2002-07-02 16:49 UTC] sterling@php.net
Actually, Jani is right, it should work this way, quoth the C manual:

If the file previously was  larger  than  this  size,  the extra  data  is lost.  If the file previously was shorter, it is extended, and the extended part reads as zero bytes.

Odd that your example doesn't show it :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 02:01:29 2024 UTC