|  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
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
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.
Block user comment
Status: Assign to:
Bug Type:
From: kaspersv at privat dot dk
New email:
PHP Version: OS:


 [2004-07-14 23:44 UTC] kaspersv at privat dot dk
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");
		function __destruct() {
			$fp = fopen("testing.txt", "a");
			fwrite($fp, "Destructor\n");
	$bugtest = new BugTest();


Expected result:
A single file containing the two lines: 


Add a Patch

Pull Requests

Add a Pull Request


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

 [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 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 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 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-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 03:03:42 2022 UTC