require_once和include_once无法正确解析文件 [英] require_once and include_once not resolving files correctly

查看:83
本文介绍了require_once和include_once无法正确解析文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有PHP的require_once,require,include_once和include函数没有正确解析文件的问题。我使用Apache 2.2.11在Windows Vista上运行PHP 5.3.1。



这是我遇到的问题:

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

返回TRUE

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

返回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'));

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



但是:

  require_once('system / application / shared / config / database.php')或die Can not Include Language Config); 
require_once('C:/wamp/www/park_factor_network/system/application/shared/config/language.php')或die(Can not Include Language Config);

返回:

 code>致命错误:require_once()[function.require]:无法在C:\wamp \www\中打开必填'1'(include_path ='; C:\php5\pear' park_factor_network\system\application\shared\hooks\select_language.php on line 25 

C:\wamp\www\park_factor_network\system\application\\\
ews_site\hooks是C:\wamp\www\park_factor_network\system\application\shared\的目录结点钩子



我只遇到这个问题,当从框架中的某个位置访问这个文件,但由于这是一个直接include或要求它不应该受到影响只有PHP?

解决方案

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



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

  require_once('system / application / shared / config / database.php')
或die(Can not Include Language Config);



$ b

 ('system / application / shared / config / database.php')
或die(Can not Include Language Config);

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



包含在加载文件失败时不会返回 false require_once()将终止脚本的执行。如果你在生产环境中关心错误报告的转换,你可以很容易地遇到一个PHP致命错误,告诉你该文件不存在(而不是你的自定义 die()如果您需要正常退出脚本,我会执行 file_exists 调用语句之前, die()时失败:

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

if((!is_file($ file))或(!is_readable($ file)))
die(Can not Include Language Config);

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


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.

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

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

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.

解决方案

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

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.

What you are doing doesn't make real sense though, because include will not return false when 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()).

If you need to gracefully exit the script, I would do a file_exists call before the statement, and die() when that fails:

$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');

这篇关于require_once和include_once无法正确解析文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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