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

Abierto
#18,234 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Feature SAPI: cli Status: Verified
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.1k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo el ejemplo de CLI con una ruta relativa usando PHP 8.4 e inspecciona cómo el punto de entrada de CLI establece SCRIPT_FILENAME. Verifica el comportamiento antes y después de chdir('/tmp'); se considera terminado cuando SCRIPT_FILENAME sigue siendo la ruta absoluta del script en ejecución al invocarlo con una ruta relativa.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
php
Área
cli
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.