php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42626 file operations in destructor cause weird results
Submitted: 2007-09-11 19:18 UTC Modified: 2008-11-10 11:14 UTC
From: admin at ifyouwantblood dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.4 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2007-09-11 19:18 UTC] admin at ifyouwantblood dot de
Description:
------------
Calling clearstatcache() in destructor causes some very weird stuff.
In the code below a file is created / truncated in the constructor. If you want to read the same file with fread in the destructor while it's still empty i'm expecting fread to print an error which it does. However if you write something to the file AFTER reading it and run the script twice fread returns what's written before. This should not happen, because before reading the file, we truncate it. Moreover if you continue calling the script, the string that fread returns contains all the writing operations done before.

The strangest thing is, that it looks like that this behavior got something to do with the filename "datei.txt" (is German, means file.txt). If you change the filename, the fopen($this->filename,'r+') after clearstatcache() fails with the error "file does not exists".

If you explicite truncate the file before reading it with ftruncate() no error is printed.

If you remove clearstatcache() in the destructor it works as expected.

This also appears on PHP 5.2.3


Reproduce code:
---------------
<pre>
<?php

#call this script more than once!

error_reporting(E_ALL|E_NOTICE);

$test=new test;

class test
{
	private $filename='datei.txt';
	
	public function __construct()
	{
		# open and truncate
		$file=fopen($this->filename,'w');
		fclose($file);

		echo 'filesize after truncate: '.filesize($this->filename)."\n";
	}

	public function __destruct()
	{
		echo 'Destructing test'."\n";
		echo 'filesize before clearstatcache: '.filesize($this->filename)."\n";

		clearstatcache();

		echo 'filesize after clearstatcache: '.filesize($this->filename)."\n";

		$file=fopen($this->filename,'r+');
		if($file)
		{
			echo 'Reading file: '.fread($file,filesize($this->filename));
			fputs($file,'aasadf');
		}
	}
}

?>

Expected result:
----------------
filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 0

Warning: fread() [function.fread]: Length parameter must be greater than 0 in G:\php\htdocs\cms\1jailbreak\admin\constructor.php on line 34

Reading file: 

Actual result:
--------------
filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 6
Reading file: aasadf

-----

on repeated calling:

filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 12
Reading file: aasadfaasadf

-----

filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0
filesize after clearstatcache: 18
Reading file: aasadfaasadfaasadf

and so on...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-12 00:14 UTC] admin at tifyouwantblood dot de
this works as expected on PHP 5.2.2 on SunOS.
 [2007-10-22 21:58 UTC] admin at ifyouwantblood dot de
hi,

now i get the following with windows 5.2.5RC2 build Oct 22 2007 12:03:56 althought the file exists (but with size 0):

--------------------------
filesize after truncate: 0
Destructing test
filesize before clearstatcache: 0

Warning:  filesize() [function.filesize]: stat failed for datei.txt in F:\php\htdocs\ALL\bug.php on line 30
filesize after clearstatcache: 

Warning:  fopen(datei.txt) [function.fopen]: failed to open stream: No such file or directory in F:\php\htdocs\ALL\bug.php on line 32
---------------------------
 [2007-10-25 19:29 UTC] admin at ifyouwantblood dot de
allright guys it should be mentioned somewhere that the destructor's base directory is the root of the server. so this 'bug' is not a bug, but a simple mistake which could have been avoided just by telling this....

the given sample searches for the file in the base directory of the server. unfortunatly i had had a file there with the name 'datei.txt' which then was read by/written to by php.

but to my reassurance none of the developers seems to know this, or else they would have said something, wouldn't they?

so, if any one ever reads this, where can i communicate with the PHP Manual team in order to tell this (and something else)?

greetings.
 [2007-11-24 12:26 UTC] jani@php.net
Reclassified as documentation issue.
 [2008-11-10 11:14 UTC] vrana@php.net
Already documented: "The working directory in the script shutdown phase can be different with some SAPIs (e.g. Apache)." at http://www.php.net/manual/en/language.oop5.decon.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 23:01:30 2024 UTC