1 回答

TA贡献1847条经验 获得超7个赞
您可以使用,$_SERVER["DOCUMENT_ROOT"]因为这将始终返回您正在处理的任何内容的根文件夹。一个示例实现:
<?php
$file = $_SERVER["DOCUMENT_ROOT"]."path/to/my/folder/needThis.php";
echo "#LOC: ".$file; //output where is the file
include $file;
#rest of code....
?>
它会返回这样的东西:
#LOC: server/path/to/my/folder/needThis.php
但是,如果您的文件被移动到服务器的子域,您需要调整受影响的文件,一种规避方法是使用 getcwd()
例如:如果您的文件被移动到子域(例如:)server/subb,上面的脚本将返回如下内容:
#LOC: server/path/to/my/folder/needThis.php
Warning: include(server/path/to/my/folder/needThis.php): failed to open stream: No such file or directory in server/subb/index.php on line 4
如果我们更换$_SERVER["DOCUMENT_ROOT"],getcwd()问题就不会发生。
采取预防措施,我不知道使用这种方法会有什么安全风险。
- 1 回答
- 0 关注
- 120 浏览
添加回答
举报