I'm trying to get the full path of the current executed file within PHP.
realpath()
returns the path without the script filename.
pathinfo()
you cannot use without passing it the whole path to break down.
__FILE__
gets the path of the calling file (ie; where the __FILE__
constant was called), which may be inside an included file
; which I don't want.
The $_SERVER
variables REQUEST_URI
, SCRIPT_NAME
& PHP_SELF
don't returns the full path.
The most promising is the $_SERVER
variable SCRIPT_FILENAME
apart from the caveat..
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.
I want exactly what $_SERVER['SCRIPT_FILENAME']
returns such as /home/site/public_html/index.php
, but just not 100% sure if using that is safe to use in all instances?
Is the latter the best method to use? Apart from the CLI, will it always return the correct path for normal web requests?