[英]PHP - include() or require() with relative paths won't work on windows, even when appending __DIR__
I was reading here about problems in PHP when using include() or required() with relative paths and all the solutions I saw was to append DIR 我在这里阅读有关PHP的问题,当使用带有相对路径的include()或required()时,我看到的所有解决方案都是附加DIR
I'm currently working on Windows, and even though the error message displays the current value of DIR , then the relative path seems to be added as a string, rather than going one level up, for example: 我目前正在使用Windows,即使错误消息显示DIR的当前值,然后相对路径似乎被添加为字符串,而不是向上升级,例如:
include(__DIR__ . "..\\another_folder\\file_2.php");
produces the following error: Warning: include(C:\\xampp\\htdocs\\main_folder..\\another_folder\\file_2.php) [function.include]: failed to open stream: No such file or directory in 产生以下错误:警告:include(C:\\ xampp \\ htdocs \\ main_folder .. \\ another_folder \\ file_2.php)[function.include]:无法打开流:没有这样的文件或目录
Any idea what's going on? 知道发生了什么事吗?
You need to add the \\
after the directory name: 您需要在目录名后添加
\\
:
include(__DIR__ . "\\..\\another_folder\\file_2.php");
This will make the path be 这将使路径成为
C:\\xampp\\htdocs\\main_folder\\..\\another_folder\\file_2.php
C:\\ XAMPP \\ htdocs中\\ main_folder \\ .. \\ another_folder \\ file_2.php
instead of 代替
C:\\xampp\\htdocs\\main_folder..\\another_folder\\file_2.php
C:\\ XAMPP \\ htdocs中\\ main_folder .. \\ another_folder \\ file_2.php
Also, for portability, it is advisable to use /
instead of \\
, which works on all platforms, including Windows: 另外,为了便于携带,建议使用
/
而不是\\
,它适用于所有平台,包括Windows:
include(__DIR__ . "/../another_folder/file_2.php");
Don't use backslashes in paths in PHP, use regular forward slashes ( /
) everywhere. 不要在PHP的路径中使用反斜杠,在任何地方使用常规正斜杠(
/
)。 PHP will translate to the appropriate OS-specific directory separators for you automatically. PHP将自动为您转换为适当的特定于操作系统的目录分隔符。
That being said, look at the error message in detail: 话虽如此,请详细查看错误消息:
... lude(C:\xampp\htdocs\main_folder..\another_folder\file_2.php) [func...
^--- missing a slash here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.