Get absolute path of current script

2019-01-04 16:23发布

I have searched high and low and get a lot of different solutions and variables containing info to get the absolute path. But they seem to work under some conditions and not under others. Is there one silver bullet way to get the absolute path of the executed script in PHP? For me, the script will run from the command line, but, a solution should function just as well if run within Apache etc.

Clarification: The initially executed script, not necessarily the file where the solution is coded.

标签: php path include
16条回答
姐就是有狂的资本
2楼-- · 2019-01-04 16:25

This is what I use and it works in Linux environments. I don't think this would work on a Windows machine...

//define canonicalized absolute pathname for the script
if(substr($_SERVER['SCRIPT_NAME'],0,1) == DIRECTORY_SEPARATOR) {
    //does the script name start with the directory separator?
    //if so, the path is defined from root; may have symbolic references so still use realpath()
    $script = realpath($_SERVER['SCRIPT_NAME']);
} else {
    //otherwise prefix script name with the current working directory
    //and use realpath() to resolve symbolic references
    $script = realpath(getcwd() . DIRECTORY_SEPARATOR . $_SERVER['SCRIPT_NAME']);
}
查看更多
狗以群分
3楼-- · 2019-01-04 16:27
`realpath(dirname(__FILE__))` 

it gives you current script(the script inside which you placed this code) directory without trailing slash. this is important if you want to include other files with the result

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-04 16:32

If you're looking for the absolute path relative to the server root, I've found that this works well:

$_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME'])
查看更多
冷血范
5楼-- · 2019-01-04 16:33

Just use below :

echo __DIR__;
查看更多
何必那么认真
6楼-- · 2019-01-04 16:36

Examples for: https://(www.)example.com/subFolder/yourfile.php?var=blabla#555

//=================================================== 
//========== self-defined SERVER variables ========== 
//=================================================== 
$_SERVER["DOCUMENT_ROOT"]                                                                      
查看更多
相关推荐>>
7楼-- · 2019-01-04 16:37

The easiest way to retrieve the absolute path of the initially executed script from that "main" script and any script included with include, require, require_once is by storing it in a constant at the beginning of the main script:

define( 'SCRIPT_ROOT', __FILE__ );

__FILE__ returns the path of the current script. Used inside an included script returns the path of the included file, not the script initially run as the OP asks:

Clarification: The initial executed script, not the file we are currently in

The solution of storing the __FILE__ into a constant is easier and faster than retrieving the path using debug_backtrace()


The solution above is suitable when there is a single "main" script that includes every other needed script, as in most web applications.

If that's not the case and there may be several "intital scripts" then to avoid redefinitions and to have the correct path stored inside the constant each script may begin with:

if( ! defined( 'SCRIPT_ROOT' ) ) {
    define( 'SCRIPT_ROOT`, __FILE__ );
}
查看更多
登录 后发表回答