PHP 警告:include_once() 无法打开 '' 以进行包含 (include_path='.;C:\xampp\php\PEAR')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13799755/
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-25 06:06:21  来源:igfitidea点击:

PHP Warning: include_once() Failed opening '' for inclusion (include_path='.;C:\xampp\php\PEAR')

php

提问by Bogdan Burym

I know this error is very common, I've tried to search google, I did the tricks to no avail. So my setup is, I have 3 directories:

我知道这个错误很常见,我试过在谷歌上搜索,但我的技巧无济于事。所以我的设置是,我有 3 个目录:

CLASSES->constants
PAGES
INITCONTROLS

-> 常量
页面
INITCONTROLS

EDIT:I've got new error:

编辑:我有新的错误:

*Warning: require_once(initcontrols/config.php) [function.require-once]: failed to open stream: No such file or directory in*

*警告:require_once(initcontrols/config.php) [function.require-once]:无法打开流:没有这样的文件或目录*

below is my fragment of code:

下面是我的代码片段:

require_once("initcontrols/config.php");

<div>
<?php 
$file = "initcontrols/header_myworks.php"; 
include_once($file); 
echo $plHeader;?>   
</div>

What is still lacking in here? Thanks for all help in advance.

这里还缺少什么?提前感谢所有帮助。

回答by Bogdan Burym

This should work if current file is located in same directory where initcontrolsis:

如果当前文件位于initcontrols所在的同一目录中,这应该可以工作:

<?php
$ds = DIRECTORY_SEPARATOR;
$base_dir = realpath(dirname(__FILE__)  . $ds . '..') . $ds;
require_once("{$base_dir}initcontrols{$ds}config.php");
?>
<div>
<?php 
$file = "{$base_dir}initcontrols{$ds}header_myworks.php"; 
include_once($file); 
echo $plHeader;?>   
</div>

回答by webnoob

The include path is set against the server configuration (PHP.ini) but the include path you specify is relative to that path so in your case the include path is (actual path in windows):

包含路径是针对服务器配置 ( PHP.ini) 设置的,但您指定的包含路径是相对于该路径的,因此在您的情况下,包含路径是(Windows 中的实际路径):

C:\xampp\php\PEAR\initcontrols\header_myworks.php

providing the path you pasted in the subject is correct. Make sure your file is located there.

提供您在主题中粘贴的路径是正确的。确保您的文件位于那里。

For more info you can getand setthe include path programmatically.

有关更多信息,您可以以编程方式获取设置包含路径。

回答by Xavier

It is because you use a relative path.

这是因为您使用了相对路径。

The easy way to fix this is by using the __DIR__magic constant, like:

解决此问题的简单方法是使用__DIR__魔术常量,例如:

require_once(__DIR__."/initcontrols/config.php");

From the PHP doc:

来自 PHP 文档:

The directory of the file. If used inside an include, the directory of the included file is returned

文件的目录。如果在包含中使用,则返回包含文件的目录

回答by Lucas Fernandez Roldan

Put in config.php this

把这个放在config.php

$config['rewrite_short_tags'] = FALSE;