Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding executing script path in CLI impossible for relative filepaths after chdir - SCRIPT_FILENAME should be the absolute path in CLI too #18234

Open
kkmuffme opened this issue Apr 3, 2025 · 1 comment

Comments

@kkmuffme
Copy link

kkmuffme commented Apr 3, 2025

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

@iluuu1994
Copy link
Member

Hi @kkmuffme. I mean, this is documented behavior, so I don't think it classifies as a bug. But it is questionable, and I don't understand why SAPIs diverge. Reclassifying as a feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants