getting the "include path" to a file in PHP
I can get the currently executing file with the __FILE__
magic PHP constant, but this gives me /var/www/vhosts/.../httpdocs/stacktrace.php
. I am trying to get the name of the file so I can pass it into a hidden form field, and then run that script again after the form is submitted.
The script whose file name I am trying to get could be named anything, it's designed to be copied and renamed to any part of a site.
The script will always be included, it'll never be 'the' executed script.
I need to be able to find the path to the file, as it would be used in an include directive (i.e. relative to the include path).
I had hoped using ini_get('include_path')
would help me (if it was /var/www/vhosts/.../httpdocs/
then I could just cut开发者_StackOverflow that part out of the script's file path and it would work), but that just gave me .:.:.:
. I don't even know what that means.
Any push in the right direction would help tonnes.
Not sure if I understood exactly what you want, but how about:
$root = $_SERVER['DOCUMENT_ROOT'];
$pathWithLeadingSlash = substr(__FILE__, strlen($root));
// or
$pathWithoutLeadingSlash = substr(__FILE__, strlen($root) + 1);
You can use one of the following values to do what you want:
$_SERVER['DOCUMENT_ROOT']
$_SERVER['SCRIPT_FILENAME']
The first returns the docroot, and the second returns the path to 'the' executed script. You can then use this information to get the relative path to the current file.
Perhaps you are looking for basename()
?
精彩评论