php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29167 fopen works differently in a constructor vs. a destructor
Submitted: 2004-07-14 23:44 UTC Modified: 2004-08-03 01:00 UTC
Votes:9
Avg. Score:4.0 ± 0.8
Reproduced:9 of 9 (100.0%)
Same Version:2 (22.2%)
Same OS:6 (66.7%)
From: kaspersv at privat dot dk Assigned: helly (profile)
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0 OS: *
Private report: No CVE-ID: None
 [2004-07-14 23:44 UTC] kaspersv at privat dot dk
Description:
------------
When I run the attached code on my machine, it creates a file called testing.txt in the directory where the php-file was located, containing the line "Constructor". And another file in the apache servers root directory also called testing.txt, containing the line "Destructor". 

Reproduce code:
---------------
<?
	
	class BugTest {
		function __construct() {
			$fp = fopen("testing.txt", "a");
			fwrite($fp, "Constructor\n");
			fclose($fp);
		}
		
		function __destruct() {
			$fp = fopen("testing.txt", "a");
			fwrite($fp, "Destructor\n");
			fclose($fp);
		}
	}
	
	$bugtest = new BugTest();

?>

Expected result:
----------------
A single file containing the two lines: 
Constructor
Destructor


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-15 09:12 UTC] helly@php.net
ATM you need to explicitly unset the object before script termination by using:
unset($bugtest);
 [2004-07-15 18:25 UTC] wez@php.net
Nothing to do with fopen, but instead the shutdown order; reclassifying.
 [2004-07-25 21:10 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-08-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-07-30 09:32 UTC] hans dot duedal at gmail dot com
As wez@php.net said, it's an shutdown order problem. Calling unset explicitly solves the problem, which also affects the unlink function.

I was trying to create a destructor that would remove a temporary file.
 [2007-07-30 09:53 UTC] hans dot duedal at gmail dot com
Upon closer examination, this bug is closely related to bug:34206, in which sniper@php.net states: "This really should be documented. Apache(1&2) does the weird
cwd change for some reason."

This obviously affect fopen calls, and although it did not seem like the problem at first, that is whats causing it. Calling destruct explicitly as wez@php.net suggested simply workaround bug:34206.

I should also state that I am running "Apache/2.2.3 (Ubuntu) PHP/5.2.1"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC