我在这里读到有关在将 include() 或 required() 与相对路径一起使用时 PHP 中出现的问题,我看到的所有解决方案都是附加 DIR
我目前在 Windows 上工作,尽管错误消息显示了 DIR 的当前值,但相对路径似乎是作为字符串添加的,而不是向上一级,例如:
include(__DIR__ . "..\\another_folder\\file_2.php");
产生以下错误: 警告:包括(C:\xampp\htdocs\main_folder..\another_folder\file_2.php)[function.include]:无法打开流:在
中没有这样的文件或目录知道发生了什么吗?
最佳答案
需要在目录名后加上\
:
include(__DIR__ . "\\..\\another_folder\\file_2.php");
这将使路径成为
C:\xampp\htdocs\main_folder\..\another_folder\file_2.php
代替
C:\xampp\htdocs\main_folder..\another_folder\file_2.php
此外,为了可移植性,建议使用 /
而不是 \
,它适用于所有平台,包括 Windows:
include(__DIR__ . "/../another_folder/file_2.php");
关于PHP - 具有相对路径的 include() 或 require() 在 Windows 上不起作用,即使在附加 __DIR__ 时也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9302364/