php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29164 Odd behavior when opening a file from __destruct()
Submitted: 2004-07-14 22:08 UTC Modified: 2004-08-05 23:28 UTC
From: riorius at hotmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.0.0 OS: Windows 2000 Pro
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: riorius at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-07-14 22:08 UTC] riorius at hotmail dot com
Description:
------------
In my script, a user-defined class is instantiated, and a reference is maintained until the script's end.  When the script ends, the class's __destruct() method is called.  Inside of the __destruct() method for this class, I was trying to open a file on the server to save data.

Reproduce code:
---------------
<?php
class Foo
{
    function __destruct()
    {
        $out = fopen("save.txt", "w");
        fwrite($out, $info);
    }
}

$temp = new Foo();
?>

Expected result:
----------------
I expected that, when the script ended, the __destruct() method would be called, and it would successfully open the file and save the data.

Actual result:
--------------
When opening a file for writing, I would get a warning, "failed to open stream: Permission denied"; if I tried to open a file for reading, I would get a different warning, "failed to open stream: No such file or directory".

Neither of these errors are appropriate: all permissions were available, the file was already created.  If I used the exact same code in another method, or destroyed the reference to the class before the script's termination (thus causing the __destruct() method to be called earlier), the file would open and write with no problems.  This only arose when the reference to my object was destroyed by the script's termination.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-14 23:53 UTC] kaspersv at privat dot dk
This bug is probably a variation of the same problem that I experienced. Try changing the permissions for the apache-root folder (e.g. C:\Program Files\Apache Group\Apache on my computer).

http://bugs.php.net/bug.php?id=29167
 [2004-07-19 20:19 UTC] riorius at hotmail dot com
Based on "kaspersv at privat dot dk"'s comment, I found a workaround.  Since the script tries to open a file in the apache root directory rather than the script's directory, the solution is to specify the entire path:

$out = fopen(dirname($_SERVER['PATH_TRANSLATED']) . "/save.txt", "w");
 [2004-08-05 23:28 UTC] edink@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 18:01:35 2024 UTC