windows require_once 和 include_once 无法正确解析文件

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

require_once and include_once not resolving files correctly

phpwindowscodeignitersymlink

提问by Ian Cant

Am having problems with PHP's require_once, require, include_once and include functions not correctly resolving a file. I am running PHP 5.3.1 on Windows Vista with Apache 2.2.11.

我遇到了 PHP 的 require_once、require、include_once 和 include 函数无法正确解析文件的问题。我在带有 Apache 2.2.11 的 Windows Vista 上运行 PHP 5.3.1。

These are the problems I am getting:

这些是我遇到的问题:

file_exists('C:/wamp/www/park_factor_network/system/application/shared/config/language.php')

returns TRUE

返回真

is_readable('system/application/shared/config/language.php')

returns TRUE

返回真

$fp = fopen('C:/wamp/www/park_factor_network/system/application/shared/config/language.php','r');
$contents = fread($fp, filesize('C:/wamp/www/park_factor_network/system/application/shared/config/language.php'));

returns a valid file resource and stores it into $contents

返回一个有效的文件资源并将其存储到 $contents

However:

然而:

require_once('system/application/shared/config/database.php') or die("Cannot Include Language Config");
require_once('C:/wamp/www/park_factor_network/system/application/shared/config/language.php') or die("Cannot Include Language Config");

return:

返回:

Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.;C:\php5\pear') in C:\wamp\www\park_factor_network\system\application\shared\hooks\select_language.php on line 25

C:\wamp\www\park_factor_network\system\application\news_site\hooks is a Directory Junction for C:\wamp\www\park_factor_network\system\application\shared\hooks

C:\wamp\www\park_factor_network\system\application\news_site\hooks 是 C:\wamp\www\park_factor_network\system\application\shared\hooks 的目录连接

I only experience this problem when accessing this file from a certain location in the framework, however as this is a direct include or require it shouldn't be affected by that and only PHP? If I try and include the file anywhere else within my setup it loads fine.

我只在从框架中的某个位置访问这个文件时遇到这个问题,但是因为这是一个直接包含或要求它不应该受那个影响,只有 PHP?如果我尝试将文件包含在我的设置中的其他任何地方,它加载得很好。

回答by Pekka

require_onceis a language construct. It is not a function and has no return value.

require_once是一种语言结构。它不是一个函数,也没有返回值。

Brackets around the file name parameter are optional. This seems to mean that in this line:

文件名参数周围的括号是可选的。这似乎意味着在这一行中:

require_once('system/application/shared/config/database.php') 
             or die("Cannot Include Language Config");

the whole expression

整个表达

('system/application/shared/config/database.php') 
 or die("Cannot Include Language Config");

is evaluated (returning 1) and used as the file name argument. 1, obviously, doesn't exist.

被评估(返回1)并用作文件名参数。1,显然,不存在。

What you are doing doesn't make real sense though, because includewill not return falsewhen loading a file fails. require_once()will terminate the script's execution anyway. If you take care of switching of error reporting in your production environment, you could easily live with a PHP Fatal Error telling you the file doesn't exist (instead of your custom die()).

但是,您正在做的事情没有实际意义,因为加载文件失败时include不会返回falserequire_once()无论如何都会终止脚本的执行。如果您在生产环境中处理错误报告的切换,您很容易接受一个 PHP 致命错误,告诉您该文件不存在(而不是您的自定义die())。

If you needto gracefully exit the script, I would do a file_existscall before the statement, and die()when that fails:

如果你需要优雅地退出脚本,我会file_exists在语句之前调用,die()当调用失败时:

$file = 'system/application/shared/config/database.php';

if ((!is_file($file)) or(!is_readable($file)))
   die("Cannot Include Language Config");

require_once('system/application/shared/config/database.php');