PHP:警告:在 Windows 上包含无法打开流没有此类文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8525423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:54:54  来源:igfitidea点击:

PHP: Warning: include failed to open stream no such file or directory on Windows

phpinclude

提问by user840930

I have an include statement in a PHP script running on xampp on Windows. If I use a relative path :

我在 Windows 上的 xampp 上运行的 PHP 脚本中有一个包含语句。如果我使用相对路径:

include '../config/eventInfoConfig.php';  

I get the error message:

我收到错误消息:

Warning: include(../config/eventInfoConfig.php) [function.include]: failed to open stream: No such file or directory

警告:include(../config/eventInfoConfig.php) [function.include]:无法打开流:没有这样的文件或目录

But if I use the absolute path I have no error:

但是如果我使用绝对路径,我就没有错误:

include 'c:/xampp/htdocs/xampp/appTrials/myApp/config/eventInfoConfig.php'; 

How can I use a relative path in my include without causing an error?

如何在我的包含中使用相对路径而不导致错误?

采纳答案by greg0ire

Fixing the include_path()to include the right path does the trick.

修复include_path()以包含正确的路径可以解决问题。

回答by Soshmo

I would make it dynamic so that you can also include this file in other files and still have it work properly.

我会使其动态化,以便您也可以将此文件包含在其他文件中,并且仍然可以正常工作。

include_once(dirname(__FILE__).'/../config/eventInfoConfig.php');

回答by Ry-

You can turn the relative path into an absolute one. includeis tricky. I always just keep a global constant, BASE_PATH, that contains the absolute path, and append the file I need to include.

您可以将相对路径变成绝对路径。include很棘手。我总是只保留一个BASE_PATH包含绝对路径的全局常量,并附加我需要包含的文件。