Finding executing script path in CLI impossible for relative filepaths after chdir - SCRIPT_FILENAME should be the absolute path in CLI too
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code:
mkdir -p /abc/def
mkdir -p /abc/xyz
cd /abc/def
php ../xyz/bar.php
/abc/xyz/bar.php
<?php
var_dump( realpath( filter_input( INPUT_SERVER, 'SCRIPT_FILENAME' ) ) );
chdir( '/tmp' );
var_dump( realpath( filter_input( INPUT_SERVER, 'SCRIPT_FILENAME' ) ) );
Resulted in this output:
string(16) "/abc/xyz/bar.php"
bool(false)
SCRIPT_FILENAME is supposed to be the
The absolute pathname of the currently executing script.
while SCRIPT_NAME is the passed path (or the path relative to DOCUMENT_ROOT ?)
https://www.php.net/manual/en/reserved.variables.server.php
The docs mention:
If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
Isn't this a bug? Bc it states it should be an absolute path, and it's obviously not. Which means the 2 are identical in CLI.
Which leads back to my original example:
due to this inconsistency, it's impossible to find the currently executing script's path in CLI, if the path passed to PHP isn't absolute and chdir() is called anywhere in the application.
This makes finding the source of a bug from an error trace almost impossible, if there are multiple directories with this file
PHP Version
8.4
Operating System
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the relative-path CLI example with PHP 8.4 and inspect how the CLI entry point sets SCRIPT_FILENAME. Verify the behavior before and after chdir('/tmp'); done means SCRIPT_FILENAME remains the absolute path of the executing script when invoked with a relative path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100