Finding executing script path in CLI impossible for relative filepaths after chdir - SCRIPT_FILENAME should be the absolute path in CLI too
Nessuno ha ancora preso questa issue.
- Lingua principale
- C
- Stelle
- 40.4k
- Fork
- 8.1k
- Merge medio
- 2g 13h
- PR unite (30g)
- 96
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’esempio CLI con un percorso relativo usando PHP 8.4 e verifica come il punto di ingresso CLI imposta SCRIPT_FILENAME. Verifica il comportamento prima e dopo chdir('/tmp'); il lavoro è completato quando SCRIPT_FILENAME rimane il percorso assoluto dello script in esecuzione quando viene invocato con un percorso relativo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- php
- Ambito
- cli
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100