#10989 [Com]: Wrong file writing
| From: | shemale-mpegs1439 at hotmail dot com | Date: | Tue, 20 Jul 2004 15:04:04 +0000 |
| Subject: | #10989 [Com]: Wrong file writing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-62899@lists.php.net to get a copy of this message | ||
ID: 10989
Comment by: shemale-mpegs1439 at hotmail dot com
Reported By: trax102 at hotmail dot com
Status: Bogus
Bug Type: Filesystem function related
Operating System: Win2K-Win98-UNIX
PHP Version: 4.0 Latest CVS (2001-05-21)
New Comment:
<a href=http://v-ripped-voyeuroral.da.ru>mpegs
shemale</a>
Previous Comments:
------------------------------------------------------------------------
[2001-05-21 14:49:17] cynic@php.net
It's not. The clearstatcache() is really needed (per manual). The
result of filesize() is cached (and it's 0), so on all the calls to
read_file() 0 bytes is read, hence
$filename="test.txt";
write_file("", $filename);
# filesize() returns 0, and the result is cached
$content = read_file($filename);
# read filesize(), i. e. the cached 0 bytes. ok this time
$content .= "ADD1";
write_file($content, $filename);
$content = read_file($filename);
# read filesize(), i. e. the cached 0 bytes. this time it's not ok
# "" == $content
$content .= "ADD2";
write_file($content, $filename);
BTW I think it should be bogusified (user error).
------------------------------------------------------------------------
[2001-05-21 11:55:50] derick@php.net
This should not be closed yet, as I think it's a valid bug.
Derick
------------------------------------------------------------------------
[2001-05-21 11:52:14] trax102 at hotmail dot com
Ok, I've found the solution: simply call clearstatcache() before
filesize().
------------------------------------------------------------------------
[2001-05-21 10:57:52] trax102 at hotmail dot com
<?php
//At the end of the script "test.txt" should
//contains "ADD1ADD2", but, as you can see,
//it will contains just "ADD2".
//Is there a work around for this problem ?
function read_file ($file_name)
{
$file = fopen($file_name, "r");
flock($file, 1);
$content = trim(fread($file, filesize($file_name)));
flock($file, 3);
fclose ($file);
return ($content);
}
function write_file ($content, $file_name)
{
$file = fopen($file_name, "w");
flock($file, 2);
fwrite($file, $content);
flock($file, 3);
fclose ($file);
}
$filename="test.txt";
write_file("", $filename);
$content = read_file($filename);
$content .= "ADD1";
write_file($content, $filename);
$content = read_file($filename);
$content .= "ADD2";
write_file($content, $filename);
?>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=10989&edit=1