php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37246 Cannot write files in __destruct()
Submitted: 2006-04-29 19:23 UTC Modified: 2006-04-30 08:27 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mass at carlsoft dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.2 OS: Kubuntu 5.10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mass at carlsoft dot net
New email:
PHP Version: OS:

 

 [2006-04-29 19:23 UTC] mass at carlsoft dot net
Description:
------------
A warning is thrown when trying to use file_put_contents in the destructor of a class.  It reports that permission was denied, but the directory and all children are chmod'd to 777.  Additionally, I can write to the file any other time in the script.

Reproduce code:
---------------
class test
{
 public function __destruct()
 {
   file_put_contents('somefile','somedata');
 }
}

$foo = new test;

Expected result:
----------------
somefile contain somedata

Actual result:
--------------
Warning with a permission error

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-30 06:38 UTC] judas dot iscariote at gmail dot com
AFAIK this is the expected behaviuor.destructors are called at script shutdown, and most parts of the engine are already deactivated.

A PHP dev can correct me if Im wrong.
 [2006-04-30 07:13 UTC] judas dot iscariote at gmail dot com
...and you get permission denied because at some point of the script shutdown, apache chdir() to SERVER_ROOT.
 [2006-04-30 08:27 UTC] mike@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Exactly.

mike@honeybadger:/www/html$ cat destrct.php
<?php
class c { 
  function __destruct() { 
    var_dump(realpath(".")); 
  }
} 
$c = new c;

Output in browser:
string(1) "/"

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 20:01:33 2025 UTC