include_once(../../ config.php):无法打开流:没有这样的文件或目录

问题描述 投票:0回答:1

我的文件路径如>>

/config.php
/fold_1/fold_2/functions.php
/fold_1/myfile.php

functions.php包含>>

include_once("../../config.php");
function addition($x,$y){return($x+$y);}
function substraction($x,$y){return($x-$y);}

myfile.php包含>>

include_once("fold_2/functions.php");
call addition(2,3);

但它会发出像>>一样的警告

include_once(../../config.php): failed to open stream: No such file or 
directory 

我知道原因就像当我在function.php中包含myfile.php它将function.php的所有代码替换为myfile.php然后运行function.php的代码所以它无法在config.php找到../../config.php文件

但我不知道有没有任何解决方案(解决方案意味着改变php.ini或类似的变量值)来克服这个问题所以它可以先运行functions.php的代码,然后它将映射到myfile.php

php apache xampp wamp
1个回答
3
投票

尝试使用这样:

include_once $_SERVER['DOCUMENT_ROOT']. '/path/to/config.php';

有时PHP和服务器更喜欢完整的绝对路径而不是../../ - 我通常使用$_SERVER修复

© www.soinside.com 2019 - 2024. All rights reserved.