简体   繁体   中英

Require_once PHP Error

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.

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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