php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74585 chroot doesn't change __DIR__ constant
Submitted: 2017-05-12 19:28 UTC Modified: 2021-04-26 16:03 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: joomla at itronic dot at Assigned:
Status: Closed Package: CGI/CLI related
PHP Version: 7.0.19 OS: Linux
Private report: No CVE-ID: None
 [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
 [2021-04-26 16:03 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-04-26 16:03 UTC] cmb@php.net
> 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.

That.  It might be best to just clearly document that chroot()
doesn't affect __DIR__ and friends.
 [2021-04-26 16:46 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/625dc345dba34bd0e8f97b6fac4272a5e8359428
Log: Fix #74585: chroot doesn't change __DIR__ constant
 [2021-04-26 16:46 UTC] git@php.net
-Status: Open +Status: Closed
 [2021-04-27 01:03 UTC] git@php.net
Automatic comment on behalf of mumumu
Revision: https://github.com/php/doc-ja/commit/7605101cdebddcda0b12bc3c67f86659b85ff8b9
Log: Fix #74585: chroot doesn't change __DIR__ constant
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 14:01:31 2024 UTC