Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with clearstatcache()

Status
Not open for further replies.

Marker67

Programmer
Joined
Dec 1, 2004
Messages
3
Location
US
The Keyword search is currently down, so I hope this wasn't answered previously.

Does anyone know if there is an issue with clearstatcache() or am I doing something wrong?

Here is a bit of linear code to illustrate my issue...
Code:
$filename = "testfile.txt";

if (! is_file($filename))
{
	echo "<b>The file: $filename does not exist.  Creating it.</b>\n<br><br>";
}

$fh = fopen($filename, "a+") or die ("The file: $filename could not be created or opened.");

$filesize = filesize($filename);
echo "<b>Filesize:</b> $filesize\n<br><br>";

if ($filesize > 0)
{ 
	$readdata = fread($fh, filesize($filename));
	echo "<b>Data before write:</b> $readdata\n<br><br>";
}
	
$writeit = fwrite ($fh, "This is a test.");

clearstatcache();  //filesize() supposedly won't work twice unless you clearstatcache()
rewind($fh);   //two freads won't work unless you rewind the pointer
$filesize = filesize($filename);
echo "<b>Filesize:</b> $filesize\n<br><br>";
$readdata = fread($fh, $filesize);
echo "<b>Data after write:</b> $readdata\n<br><br>";


rewind($fh);
$readdata = fread($fh, 1024);
echo "<b>Data after write with specific read size:</b> $readdata\n<br><br>";

fclose ($fh);

$str = file_get_contents($filename);
echo "<b>This is what is REALLY in the $filename file:</b> ".$str."<br>";


When the file doesn't exist, this is what I get...

The file: testfile.txt does not exist. Creating it.

Filesize: 0

Filesize: 0


Warning: fread() [function.fread]: Length parameter must be greater than 0. in C:\Program Files\Apache Group\Apache2\htdocs\testing\file_io2.php on line 27
Data after write:

Data after write with specific read size: This is a test.

This is the data that is REALLY in the testfile.txt file: This is a test.



When the file does exist, this is what I get...

Filesize: 15

Data before write: This is a test.

Filesize: 15

Data after write: This is a test.

Data after write with specific read size: This is a test.This is a test.

This is what is REALLY in the testfile.txt file: This is a test.This is a test.
 
I just copied your code and executed it without any errors on a UNIX type OS. The output is just as expected.

However, then I did the same thing with a Windows XP machine, Apache. Won't work. This seems to be another Windows specific issue.
It is somewhat insufficient - but it seems that Windows will not update the filesize until you actually close the filehandle. I closed the filehandle and it works fine.
 
Cool. Thanks for the confirmation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top