ID: 29503
Updated by: [EMAIL PROTECTED]
Reported By: blacklight at samtelecom dot ru
-Status: Open
+Status: Bogus
Bug Type: Filesystem function related
Operating System: unix
PHP Version: 4.3.7
New Comment:
using fseek() and fwrite() on a file opened in append mode will lead to
undefined results.
Don't do it.
Previous Comments:
------------------------------------------------------------------------
[2004-08-03 08:31:40] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=29503&edit=1