php / php/php-src

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

Offen
#18,234 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Feature SAPI: cli Status: Verified
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das CLI-Beispiel für relative Pfade mit PHP 8.4 zu reproduzieren, und untersuche, wie der CLI-Einstiegspunkt SCRIPT_FILENAME setzt. Überprüfe das Verhalten vor und nach chdir('/tmp'); fertig ist die Aufgabe, wenn SCRIPT_FILENAME beim Aufruf mit einem relativen Pfad der absolute Pfad des ausgeführten Skripts bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.