php 获取初始运行脚本的绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4645082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:50:04  来源:igfitidea点击:

Get absolute path of initially run script

phppathinclude

提问by inquam

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.

我搜索了高低,并获得了许多不同的解决方案和包含信息的变量以获取绝对路径。但它们似乎在某些条件下起作用,而在其他条件下不起作用。有没有一种灵丹妙药的方法来获取 PHP 中执行脚本的绝对路径?对我来说,脚本将从命令行运行,但是,如果在 Apache 等中运行,解决方案应该也能正常运行。

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

说明:最初执行的脚本,不一定是解决方案编码的文件。

采纳答案by Salman A

The correct solution is to use the get_included_filesfunction:

正确的解决方法是使用get_included_files函数:

list($scriptPath) = get_included_files();

This will give you the absolute path of the initial script even if:

即使在以下情况下,这也会为您提供初始脚本的绝对路径:

  • This function is placed inside an included file
  • The current working directory is different from initial script's directory
  • The script is executed with the CLI, as a relative path
  • 这个函数被放置在一个包含文件中
  • 当前工作目录与初始脚本的目录不同
  • 该脚本使用 CLI 作为相对路径执行


Here are two test scripts; the main script and an included file:

这是两个测试脚本;主脚本和包含的文件:

# C:\Users\Redacted\Desktop\main.php
include __DIR__ . DIRECTORY_SEPARATOR . 'include.php';
echoScriptPath();

# C:\Users\Redacted\Desktop\include.php
function echoScriptPath() {
    list($scriptPath) = get_included_files();
    echo 'The script being executed is ' . $scriptPath;
}

And the result; notice the current directory:

结果; 注意当前目录:

C:\>php C:\Users\Redacted\Desktop\main.php
The script being executed is C:\Users\Redacted\Desktop\main.php

回答by zerkms

__FILE__constant will give you absolute path to current file.

__FILE__常量将为您提供当前文件的绝对路径。

Update:

更新

The question was changed to ask how to retrieve the initially executed script instead of the currently running script. The only (??) reliableway to do that is to use the debug_backtracefunction.

问题已更改为询问如何检索最初执行的脚本而不是当前正在运行的脚本。唯一 (??)可靠的方法是使用该debug_backtrace函数。

$stack = debug_backtrace();
$firstFrame = $stack[count($stack) - 1];
$initialFile = $firstFrame['file'];

回答by T.Todua

回答by rik

echo realpath(dirname(__FILE__));

If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace __FILE__with $_SERVER['PHP_SELF']. But be aware that PHP_SELF is a security risk!

如果你把它放在一个包含文件中,它会打印这个包含的路径。要获取父脚本的路径,请替换__FILE__$_SERVER['PHP_SELF']. 但请注意 PHP_SELF 存在安全风险!

回答by Gottlieb Notschnabel

__DIR__

From the manual:

手册

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FILE__always contains an absolute path with symlinks resolved whereas in older versions (than 4.0.2) it contained relative path under some circumstances.
__FILE__始终包含解析符号链接的绝对路径,而在旧版本(4.0.2 之前)中,它在某些情况下包含相对路径。

Note: __DIR__was added in PHP 5.3.0.

注意__DIR__是在 PHP 5.3.0 中添加的。

回答by pkarecki

If you want to get current working directoryuse getcwd()

如果要获取当前工作目录,请使用getcwd()

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.getcwd.php

__FILE__will return path with filename for example on XAMPP C:\xampp\htdocs\index.phpinstead of C:\xampp\htdocs\

__FILE__将返回带有文件名的路径,例如在 XAMPP 上C:\xampp\htdocs\index.php而不是C:\xampp\htdocs\

回答by Sultanos

dirname(__FILE__) 

will give the absolute routeof the current file from which you are demanding the route, the route of your server directory.

将给出 您要求该路径的当前文件的绝对路径,即您的服务器目录的路径。

example files :

示例文件:

www/http/html/index.php ; if you place this code inside your index.php it will return:

www/http/html/index.php ; 如果将此代码放在 index.php 中,它将返回:

<?php echo dirname(__FILE__); // this will return: www/http/html/

<?php echo dirname(__FILE__); // this will return: www/http/html/

www/http/html/class/myclass.php ; if you place this code inside your myclass.php it will return:

www/http/html/class/myclass.php ; 如果您将此代码放在 myclass.php 中,它将返回:

<?php echo dirname(__FILE__); // this will return: www/http/html/class/

<?php echo dirname(__FILE__); // this will return: www/http/html/class/

回答by Matricore

Just use below :

只需在下面使用:

echo __DIR__;

回答by minhajul

`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

它为您提供当前脚本(您放置此代码的脚本)目录,而没有尾部斜杠。如果您想在结果中包含其他文件,这很重要

回答by SiteKickr

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'])