PHP 从 webroot 外部的文件中包含 webroot 中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13550471/
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 file in webroot from file outside webroot
提问by user1662927
I have a php file outside my webroot in which I want to include a file that is inside the webroot.
我在我的 webroot 之外有一个 php 文件,我想在其中包含一个位于 webroot 内的文件。
folder outside webroot
- > php file in which I want to include
webroot
- > file to include
webroot 之外的文件夹
- > 我想在其中包含
webroot 的php 文件
- > 要包含的文件
So I have to go one directory up, but this doesnt work:
所以我必须上一个目录,但这不起作用:
include('../webroot/file-to-include.php');
Include full path doesn't work either:
包含完整路径也不起作用:
include('home/xx/xx/domains/mydomain/webroot/file-to-include.php');
How can I accomplish this?
我怎样才能做到这一点?
回答by xdazz
Full path should be:
完整路径应该是:
include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');
Or you should set the path like:
或者您应该将路径设置为:
include(__DIR__ . '/../webroot/file-to-include.php'); // php version >= 5.3
include(dirname(__FILE__) . '/../webroot/file-to-include.php'); // php version < 5.3
回答by didierc
Have this in a common file, shared by all your php sources outside the webroot:
将它放在一个公共文件中,由 webroot 之外的所有 php 源共享:
<?php
define('PATH_TO_WEBROOT', '/home/xx/xx/domains/mydomain/webroot');
And then use the following to include files.
然后使用以下内容来包含文件。
<?php
include (PATH_TO_WEBROOT.'/file-to-include.php');
If the location of your webroot changes, you will only have to change that once in your code base.
如果您的 webroot 的位置发生变化,您只需在代码库中更改一次。
You can configure php to automatically prependa given file to all your scripts, by setting the auto_prepend_filedirective. That file could for instance contain the PATH_TO_WEBROOTconstant, or require_oncethe file which contains it. This setting can be done on a per domain or per host basis (see the ini sections documentation).
您可以通过设置指令将 php 配置为自动将给定文件添加到所有脚本中auto_prepend_file。例如,该文件可以包含PATH_TO_WEBROOT常量,或require_once包含它的文件。此设置可以在每个域或每个主机的基础上完成(请参阅ini 部分文档)。
Also, consider using the autoloadfeature if you are using classes extensively.
此外,如果您广泛使用类,请考虑使用自动加载功能。
回答by al_qaysee
I put the secured data in the file named conn.txt,
我将受保护的数据放在名为 的文件中conn.txt,


and then I used the following PHP command:
然后我使用了以下 PHP 命令:
$DbInfoFile = "../conn.txt";
回答by David Müller
Try prepending a trailing slash to your full path, so it looks like
尝试在完整路径前添加一个斜杠,看起来像
include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');
include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');
Otherwise, it will be interpreted as a relative path.
否则,它将被解释为相对路径。
You could also try to change the dirinto the webroot and see if this works - for debuggign purposes:
您也可以尝试将目录更改为 webroot 并查看这是否有效 - 用于调试目的:
chdir("/home/xx/xx/domains/mydomain/webroot");
include "your_file.php";
回答by wadie
This should work
这应该工作
$_SERVER['DOCUMENT_ROOT']/home/xx/xx/domains/mydomain/webroot/file-to-include.php
And make sure you have access to that level.
并确保您可以访问该级别。

