php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43729 Fread isnt able to read
Submitted: 2008-01-02 11:22 UTC Modified: 2008-01-05 16:20 UTC
From: rc at opelgt dot org Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.4.7 OS: Mac OS 10.4.11
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: rc at opelgt dot org
New email:
PHP Version: OS:

 

 [2008-01-02 11:22 UTC] rc at opelgt dot org
Description:
------------
Although the pointer is at position 0 and filesize is cached with the 
value of 4 fread reads nothing.

A clearstatcache is necessary for fread to operate correct.

Reproduce code:
---------------
<?php
$str1 = '1234';
$str2 = '56';
$datei = 'test.txt';

$dh = fopen ($datei,"w");
fwrite($dh, $str1);
fclose($dh);

$dh = fopen ($datei,"r+");
echo 'content: '.fread($dh, filesize($datei))."<BR>\n";
echo 'pointer after write at: '.ftell($dh).'<br>';
ftruncate($dh, '0');
echo 'pointer after truncate at: '.ftell($dh).'<br>';
echo 'filesize after truncate is: '.filesize($datei).'<br>';
fwrite($dh, $str2);
echo 'pointer after second write at: '.ftell($dh).'<br>';
rewind($dh);
echo 'pointer after rewind at: '.ftell($dh).'<br>';
echo 'content: '.fread($dh, filesize($datei)).'<br>';
clearstatcache();
echo 'content: '.fread($dh, filesize($datei));
fclose($dh);
?>

Expected result:
----------------
content: 1234
pointer after write at: 4
pointer after truncate at: 4
filesize after truncate is: 4
pointer after second write at: 6
pointer after rewind at: 0
content: 56
content: 56

Actual result:
--------------
content: 1234
pointer after write at: 4
pointer after truncate at: 4
filesize after truncate is: 4
pointer after second write at: 6
pointer after rewind at: 0
content: 
content: 56

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-05 16:20 UTC] dsp@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Sorry, but this is expected behaviour. filesize caches the size (in that case 4). You write to byte 5 and 6 when doing fwrite($dh, $str2) bust just read bye 0 to 4 when doing the last fread before cleaning the cache.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 08 20:01:30 2024 UTC