php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31570 PHP unable to resolve relative paths in __destruct() upon unloading page
Submitted: 2005-01-16 14:43 UTC Modified: 2005-05-02 01:00 UTC
Votes:24
Avg. Score:4.5 ± 0.7
Reproduced:24 of 24 (100.0%)
Same Version:2 (8.3%)
Same OS:5 (20.8%)
From: twadzilla at gmail dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5CVS-2005-01-16 OS: Redhat 9
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-01-16 14:43 UTC] twadzilla at gmail dot com
Description:
------------
PHP generates a warning inside a class destructor when you try to read a file, because apparently it cannot resolve relative paths by the time when the destructor is called for referenced objects.

When the open_basedir config is commented out, only the "include" directive resolves the relative path; the other file-reading methods fail.

When open_basedir is in effect, it causes all the methods to fail, including the "include" directive.

The following issue is demonstrated at:
http://test.kneetoe.com/foo.php
http://test.kneetoe.com/foo.php.txt
(alternatively with more checks):
http://test.kneetoe.com/foo2.php
http://test.kneetoe.com/foo2.php.txt

I recommend that class destructors get executed before the ability to resolve relative paths gets unloaded.

http://test.kneetoe.com/phpinfo.php

Reproduce code:
---------------
[httpd.conf]:
php_admin_value open_basedir /home/test/www

[/home/test/www/bar.html]:
Bar!

[/home/test/www/foo.php]:
class Foo {
    function __destruct() {
        $relative = 'bar.html';
        $absolute = "/home/test/www/$abs";
        echo "Absolute 'include': ";
        include $absolute;
        echo "Relative 'include': ";
        include $relative;
        echo "Absolute 'readfile': ";
        readfile($absolute);
        echo "Relative 'readfile': ";
        readfile($relative);
    }
}
print "<h2>Non-referenced (immediate destruction):</h2>";
new Foo;
print "<h2>Referenced (delayed destruction):</h2>";
$a = new Foo;

Expected result:
----------------
Non-referenced (immediate destruction):
Absolute 'include': Bar!
Relative 'include': Bar!
Absolute 'readfile': Bar!
Relative 'readfile': Bar!

Referenced (delayed destruction):
Absolute 'include': Bar!
Relative 'include': Bar!
Absolute 'readfile': Bar!
Relative 'readfile': Bar!


Actual result:
--------------
[[[open_basedir commented out]]]:

Non-referenced (immediate destruction):
Absolute 'include': Bar!
Relative 'include': Bar!
Absolute 'readfile': Bar!
Relative 'readfile': Bar!

Referenced (delayed destruction):
Absolute 'include': Bar!
Relative 'include': Bar!
Absolute 'readfile': Bar!
Relative 'readfile': 

Warning:  readfile(bar.html) [function.readfile]: failed to open stream: No such file or directory in /home/test/www/foo.php on line 19



[[[open_basedir in effect]]]:

Non-referenced (immediate destruction):
Absolute 'include': Bar!
Relative 'include': Bar!
Absolute 'readfile': Bar!
Relative 'readfile': Bar!

Referenced (delayed destruction):
Absolute 'include': Bar!
Relative 'include': 

Warning:  Foo::__destruct() [function.--destruct]: open_basedir restriction in effect. File(./bar.html) is not within the allowed path(s): (/home/test/www) in /home/test/www/foo.php on line 13



Warning:  Foo::__destruct(bar.html) [function.--destruct]: failed to open stream: Operation not permitted in /home/test/www/foo.php on line 13



Warning:  Foo::__destruct() [function.include]: Failed opening 'bar.html' for inclusion (include_path='.:/usr/local/lib/php') in /home/test/www/foo.php on line 13

Absolute 'readfile': Bar!
Relative 'readfile': 

Warning:  readfile() [function.readfile]: open_basedir restriction in effect. File(bar.html) is not within the allowed path(s): (/home/test/www) in /home/test/www/foo.php on line 19



Warning:  readfile(bar.html) [function.readfile]: failed to open stream: Operation not permitted in /home/test/www/foo.php on line 19

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-19 08:42 UTC] twadzilla at gmail dot com
The line in foo.php: $absolute = "/home/test/www/$abs";
should be: $absolute = "/home/test/www/$relative";
 [2005-04-24 16:50 UTC] sniper@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


 [2005-05-02 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".
 [2006-05-25 01:19 UTC] paul at stunning-stuff dot com
Hi,

I'm using PHP 5.1.4 and this bug still exists.
Including or requiring a file in the destructor using a relative path spits out an error.

It seems to me that any user-made code, including destructors should always be called before PHP cleans up anything.

Thanks,

Paul
 [2008-04-24 17:32 UTC] inspiretheweb at googlemail dot com
This problem still exists in PHP 5.2.5 (Mac OS X, Apache 2.2), and is quite a nuisance to be honest.

It seems to be due to the fact that the current working directory changes to '/' before object-destructors are called (at least under Apache).

Although this is noted at http://php.net/manual/en/language.oop5.decon.php, I think that object destructors should be called before any changes are made to the current working directory.

For developers who are unaware of this behavior, it is going to be quite difficult for them to debug the issue in their application. If for any reason this behavior is not going to change, it should at least be displayed as a warning in the PHP manual, rather than a small notice.
 [2009-10-24 19:37 UTC] wooptoo at gmail dot com
I am experiencing this bug too in PHP 5.3.0 on Linux x86_64.
I run Apache with PHP as a module. PHP changes root to / on __destruct.
 [2011-09-20 17:30 UTC] tynach2 at gmail dot com
Hi, I'm having the same issue in 5.3.3, however I can 'include' a file inside a destructor, but I cannot include a file inside of the included file inside of the destructor.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Thu Mar 30 14:01:48 2017 UTC