PHP 包含相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17407664/
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
PHP include relative path
提问by George B
I have file /root/update/test.php. There's also a file, /root/connect.php; This file has a line
我有文件/root/update/test.php。还有一个文件,/root/connect.php;这个文件有一行
include "../config.php";
In /root/update/test.php. There's the code
在 /root/update/test.php 中。有代码
set_include_path(".:/root");
include "connect.php";
When I run /root/update/test.php, it finds connect.php, but fails to find config.php, giving me
当我运行 /root/update/test.php 时,它找到了 connect.php,但找不到 config.php,给了我
PHP Warning: include(../config.php): failed to open stream: No such file or directory in /root/connect.php on line 2
PHP Warning: include(): Failed opening '../config.php' for inclusion (include_path='.:/root')
This is confusing to me because the warnings make it seem like I'm doing everything correctly - the include path is /root, and it's looking for file ../config.php (/config.php), which exists. Can someone clear this up for me? Note that using absolute paths is not an option for me, due to deploying to a production server that I have no access to.
这让我感到困惑,因为这些警告让我觉得我做的一切都正确 - 包含路径是 /root,它正在寻找存在的文件 ../config.php (/config.php)。有人可以帮我解决这个问题吗?请注意,由于部署到我无权访问的生产服务器,因此使用绝对路径不是我的选择。
Ubuntu/Apache
Ubuntu/阿帕奇
回答by meiamsome
You could always include it using __DIR__
:
您始终可以使用__DIR__
以下方法包含它:
include(dirname(__DIR__).'/config.php');
__DIR__
is a 'magical constant' and returns the directory of the current file without the trailing slash. It's actually an absolute path, you just have to concatenate the file name to __DIR__
. In this case, as we need to ascend a directory we use PHP's dirname
which ascends the file tree, and from here we can access config.php
.
__DIR__
是一个“魔法常量”,返回当前文件的目录,不带斜杠。它实际上是一个绝对路径,您只需要将文件名连接到__DIR__
. 在这种情况下,因为我们需要提升一个目录,所以我们使用 PHP 来提升dirname
文件树,从这里我们可以访问config.php
.
You could set the root path in this method too:
您也可以在此方法中设置根路径:
define('ROOT_PATH', dirname(__DIR__) . '/');
in test.php would set your root to be at the /root/
level.
在 test.php 中会将您的根设置为该/root/
级别。
include(ROOT_PATH.'config.php');
Should then work to include the config file from where you want.
然后应该努力从您想要的位置包含配置文件。
回答by Jason McCreary
While I appreciate you believe absolute paths is not an option, it isa better option than relative paths and updating the PHP include path.
虽然我很感激您认为绝对路径不是一种选择,但它是比相对路径和更新 PHP 包含路径更好的选择。
Use absolute paths with an constant you can set based on environment.
使用具有可以根据环境设置的常量的绝对路径。
if (is_production()) {
define('ROOT_PATH', '/some/production/path');
}
else {
define('ROOT_PATH', '/root');
}
include ROOT_PATH . '/connect.php';
As commented, ROOT_PATH
could also be derived from the current path, $_SERVER['DOCUMENT_ROOT']
, etc.
正如所评论的,ROOT_PATH
也可以从当前路径中导出$_SERVER['DOCUMENT_ROOT']
,等等。
回答by Mohamed Ali Hammami
function relativepath($to){
$a=explode("/",$_SERVER["PHP_SELF"] );
$index= array_search("$to",$a);
$str="";
for ($i = 0; $i < count($a)-$index-2; $i++) {
$str.= "../";
}
return $str;
}
Here is the best solution i made about that, you just need to specify at which level you want to stop, but the problem is that you have to use this folder name one time.
这是我对此提出的最佳解决方案,您只需要指定要停止的级别,但问题是您必须一次使用此文件夹名称。