php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74585 chroot doesn't change __DIR__ constant
Submitted: 2017-05-12 19:28 UTC Modified: 2017-05-12 20:40 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: joomla at itronic dot at Assigned:
Status: Open Package: CGI/CLI related
PHP Version: 7.0.19 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-05-12 19:28 UTC] joomla at itronic dot at
Description:
------------
If you change the root dir, the __DIR__ doesn't reflect the change. Maybe this effects other Magic constants.

Test script:
---------------
chroot('/tmp');
echo __DIR__;

Expected result:
----------------
__DIR__ should have the correct path.

Actual result:
--------------
The old path

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-12 20:29 UTC] dm@php.net
-Status: Open +Status: Not a bug
 [2017-05-12 20:29 UTC] dm@php.net
> __DIR__	The directory of the file.

It is not affected by chdir(), as expected, maybe you're looking for getcwd().
 [2017-05-12 20:40 UTC] requinix@php.net
-Status: Not a bug +Status: Open
 [2017-05-12 20:40 UTC] requinix@php.net
It's chroot, not chdir. The idea is that __DIR__ and friends should be updated to reflect the new path to the file after the chroot(2) call. Which is not necessarily possible (eg, file is outside the new root) so I don't know if anything can/should be done about it anyways.

echo __FILE__; // originally /path/to/file.php
chroot("/path");
echo __FILE__; // now /to/file.php
 [2017-05-12 21:16 UTC] joomla at itronic dot at
In my current use case I execute chroot in auto_prepend_file so I'm not sure when the __FILE__ constant is filled.

The reason for this is to execute customer cronjobs in a jail. And most frameworks uses __DIR__ to require other files like autoload.php...

In this case it seams that the executed file get loaded (and __DIR__ is set) before the auto_prepend_file is executed. I'm not sure if it would be possible that at least this can be fixed.
 [2017-05-12 22:48 UTC] spam2 at rhsoft dot net
No -__DIR__ reflects the physical directory where the current file is stored and is practically the same as __FILE__ without the basename - it is known at compile time and any change after that would break half of the world in existing code
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Oct 15 23:01:26 2019 UTC