PHP - include()或require()具有相对路径将无法在Windows上工作,即使附加__DIR__ [英] PHP - include() or require() with relative paths won't work on windows, even when appending __DIR__

查看:144
本文介绍了PHP - include()或require()具有相对路径将无法在Windows上工作,即使附加__DIR__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里读到关于PHP的问题,当使用带有相对路径的include()或者required()时,我看到的所有解决方案都是附加 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

我目前正在使用Windows,即使错误消息显示 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:

include(__DIR__ . "..\\another_folder\\file_2.php");

产生以下错误:
警告:include(C:\ xampp \ htdocs \ main_folder..\another_folder\file_2.php)[function.include]:无法打开流:没有这样的文件或目录

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

知道发生了什么在?

推荐答案

您需要在目录后添加 \ 名称:

You need to add the \ after the directory name:

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

而不是


C:\ xampp \htdocs \ main_folder..\another_folder \ file_2.php

C:\xampp\htdocs\main_folder..\another_folder\file_2.php

另外,为了便于携带,建议使用 / 而不是 \ ,适用于所有平台,包括Windows:

Also, for portability, it is advisable to use / instead of \, which works on all platforms, including Windows:

include(__DIR__ . "/../another_folder/file_2.php");

这篇关于PHP - include()或require()具有相对路径将无法在Windows上工作,即使附加__DIR__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆