PHP 从 webroot 之外的文件中包含 webroot 中的文件

标签 php include

我的 webroot 外有一个 php 文件,我想在其中包含 webroot 内的文件。

folder outside webroot
- > php file in which I want to include
webroot
- > file to include

所以我必须向上移动一个目录,但这不起作用:

include('../webroot/file-to-include.php');

包括完整路径也不起作用:

include('home/xx/xx/domains/mydomain/webroot/file-to-include.php');

我怎样才能做到这一点?

最佳答案

完整路径应该是:

include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');

或者你应该像这样设置路径:

include(__DIR__ . '/../webroot/file-to-include.php');  // php version >= 5.3
include(dirname(__FILE__) . '/../webroot/file-to-include.php');  // php version < 5.3

关于PHP 从 webroot 之外的文件中包含 webroot 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13550471/

相关文章:

php myqli_connect 失败。服务器请求客户端未知的身份验证方法 [caching_sha2_password]

php - 2个不同表的SQL查询

php - 无法在 PHP 的 docker 镜像中安装 JSON 扩展

php - 如何从 include() 文件中创建相对于不同 PHP 文档的链接?

c++ - CMake,前向声明,信号和 'No such file or directory'

c++ - #include<cassert> 做什么/它能做什么

c++ - CMake:根据其他人的子项目 header 构建子项目

php - 无法在命令行上运行PHP exec()

php - 在一维数组中找到 k 最长的序列?

在 cmake 中包含头文件