php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29503 fseek & fwrite error
Submitted: 2004-08-03 08:31 UTC Modified: 2004-08-03 11:26 UTC
From: blacklight at samtelecom dot ru Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.7 OS: unix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: blacklight at samtelecom dot ru
New email:
PHP Version: OS:

 

 [2004-08-03 08:31 UTC] blacklight at samtelecom dot ru
Description:
------------
multiple calls of fwrite() do not store data in the file, except last call, when opened large files (about 16mb) with fopen(filename, 'a+b')
This error does not appear when files opened with fopen(filename, 'r+b');

Reproduce code:
---------------
define(BUFFER_SIZE, 65536);
  function fCut($f, $pos, $size) {
    $fst = fstat($f);
    $fs = $fst["size"];
    if (($pos < 0) || ($pos >= $fs) || ($size <= 0)) return false;
    $ps = $pos;
    $sz = $fs - $ps - $size;
    while ($sz > 0) {
      $s = $sz; if ($s > BUFFER_SIZE) { $s = BUFFER_SIZE; }
      fseek($f, $ps + $size);
      $buf = fread($f, $s);
      fseek($f, $ps);
      $l = fwrite($f, $buf);
      $sz -= $s; $ps += $s;
    }
    ftruncate($f, max($fs - $size, $pos));
    return true;
  } // fCut - cutting $size bites in $f, from $pos

  $f = fopen(filename, 'a+b');
  flock($f, 2);
  fcut($f, 0, 123456);
  flock($f, 3);
  fclose($f);

Expected result:
----------------
123456 bites of file 'filename' must be deleted from position 0;

Actual result:
--------------
all data still the same, except block that transfered with last calls of fread/fwrite.
Note: this bug appears only in 'a+b' mode, but in 'r+b' all works fine.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-03 11:26 UTC] wez@php.net
using fseek() and fwrite() on a file opened in append mode will lead to undefined results.
Don't do it.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 28 22:01:27 2024 UTC