Require_once PHP错误 [英] Require_once PHP Error

查看:128
本文介绍了Require_once PHP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在PHP编程几年了,从未遇到这个错误。



这是我的 php 档案:

  require_once('fruit.php'); 
echo我正在编译就好了!

和我的 fruit.php 文件:

  $ bVar = true; 

当这两个文件看起来像这个^然后一切编译没有错误,我得到 >

现在,我移动 fruit.php 文件一个目录级别,并更改我的 widget.php 文件以反映目录重组:

  require_once ('../fruit.php'); 
echo我正在编译就好了!



现在突然,我得到PHP警告&致命错误:

 警告:require_once(../ fruit.php)[function.require-once]:无法打开流:在第1行的/webroot/app/widget.php中没有这样的文件或目录

致命错误:require_once()[function.require]:无法打开需要'../fruit.php' include / path ='。:/ usr / local / php5 / lib / php')in /webroot/app/widget.php第1行

在我使用PHP的所有年份中,我从未看到require_once()失败像这样。任何想法?!?!

解决方案

也许你在错误的工作目录。它的一个坏主意,依靠它(除非你明确地想访问它)反正。使用

 需要__DIR__。 '/../fruit.php'; 

或与5.3之前

  require dirname(__ FILE__)。 '/../fruit.php'; 

提醒,以 .. ,或。是不是解析的include-path,但只对当前工作目录。


I've been programming in PHP for several years now and never encountered this error before.

Here's my widget.php file:

require_once('fruit.php');
echo "I am compiling just fine!!!";

And my fruit.php file:

$bVar = true;

When these two files look like this ^ then everything compiles with no errors and I get the "I am compiling just fine!!!" success message.

Now, the minute I move the fruit.php file one directory level up, and change my widget.php file to reflect the directory restructuring:

require_once('../fruit.php');
echo "I am compiling just fine!!!";

Now all the sudden, I get PHP warnings & fatal errors:

Warning: require_once(../fruit.php) [function.require-once]: failed to open stream: No such file or directory in /webroot/app/widget.php on line 1

Fatal error: require_once() [function.require]: Failed opening required '../fruit.php' (include_path='.:/usr/local/php5/lib/php') in /webroot/app/widget.php on line 1

In all my years working with PHP, I've never seen require_once() fail like this before. Any ideas?!?!

解决方案

Maybe you are in the wrong work directory. Its a bad idea to rely on it (except you explictly want to access it) anyway. Use

require __DIR__ . '/../fruit.php';

or with pre-5.3

require dirname(__FILE__) . '/../fruit.php';

Remind, that paths starting with .., or . are not resolved against the include-path, but only against the current work directory.

这篇关于Require_once PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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