简体   繁体   English

Require_once PHP 错误

[英]Require_once PHP Error

I've been programming in PHP for several years now and never encountered this error before.我已经在 PHP 编程好几年了,以前从未遇到过这个错误。

Here's my widget.php file:这是我的widget.php文件:

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

And my fruit.php file:还有我的fruit.php文件:

$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:现在,在我将fruit.php文件向上移动一级目录的那一刻,并更改我的widget.php文件以反映目录重组:

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

Now all the sudden, I get PHP warnings & fatal errors:现在突然间,我收到 PHP 警告和致命错误:

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.在我与 PHP 合作的这些年里,我从未见过 require_once() 像这样失败。 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或使用 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.针对包含路径进行解析,而仅针对当前工作目录进行解析。

Remember that in the second case, you're specifying a path, but in the first case it uses your includes path.请记住,在第二种情况下,您指定了一个路径,但在第一种情况下,它使用您的包含路径。 Perhaps rather than explicitly specifying.. in the second case, you case modify your include path.也许不是明确指定.. 在第二种情况下,您可以修改您的包含路径。

http://php.net/manual/en/function.set-include-path.php http://php.net/manual/en/function.set-include-path.php

I know the question is old, but it is still relevant.我知道这个问题很老,但它仍然是相关的。

In my experience, the "open_basedir" directive is most likely to cause this issue.根据我的经验, “open_basedir”指令最有可能导致此问题。

require_once('fruit.php');

This searches for fruit.php in the same directory as widget.php is in, no matter what the current working directory is.无论当前工作目录是什么,这都会在与 widget.php 所在的同一目录中搜索fruit.php。

require_once('../fruit.php');

This searches for fruit.php in a directory above the current directory, not in the directory above the one widget.php is in.这会在当前目录上方的目录中搜索fruit.php,而不是在一个widget.php所在的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM