10

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?!?!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Kim
  • 111
  • 1
  • 1
  • 3
  • 5
    The only thing I can immediately think of, have you checked the webserver user's permissions for the parent folder? You may want to confirm the operating system, webserver, etc. I can see that being relevant in this case. – Jeff Parker Jul 11 '11 at 11:04
  • +1 for Jeff ... does the web server "user" have access to the parent directory. – sdolgy Jul 11 '11 at 11:05
  • @Jeff Parker - no that never crossed my mind; I would have to call up the host and ask. Would this be a variable in Apache or PHP? Which file and variable name? And thanks! – Kim Jul 11 '11 at 11:07
  • Oh, and heres the other thing: the behavior is the *exact same* if I keep fruit.php in the same directory as widget.php but just change require_once('fruit.php'); to require_once('./fruit.php');... so I don't think its a directory permissions issue... – Kim Jul 11 '11 at 11:10
  • A small tip. Thy shalt first always check the file permissions when thee finds "failed opening xx file error". Most probably its a permission error. Can the user, mostly it will be Apache if its for web, run the file `fruit.php`? – Kumar Jul 11 '11 at 11:12
  • @Kumar - Yes fruit.php is something I wrote and has no problem executing/compiling as long as its in the same directory as widget.php – Kim Jul 11 '11 at 11:15
  • Have you tried using `__DIR__` to require files using an absolute path instead of a relative path ? – Artefact2 Jul 11 '11 at 11:19
  • 1
    @kim, you didn't get my point. Are you running `widget.php` through Apache or is it a CLI script? You must understand the way PHP scripts run, on CLI/command line PHP runs a PHP script but when you use the same script for a webpage a webserver executes that script. That webserver exists as a user on *nix systems, for e.g. on my machine its `www-data`. Moreover can you do a `ls -al fruit.php` on shell and check what are the permissions set on this script? – Kumar Jul 11 '11 at 11:21
  • Is `fruit.php` still within the webroot of your server? – marchaos Jul 11 '11 at 11:07
  • You can require() or include() things outside your webroot. – Scott C Wilson Jul 11 '11 at 11:09
  • @marchaos - Yes both of these files are actually buried pretty deep within my webroot – Kim Jul 11 '11 at 11:11
  • is there something funky with the permissions? – Nick Maroulis Jul 11 '11 at 11:06
  • I don't think so; please see my last comment under the original post – Kim Jul 11 '11 at 11:12
  • So, I am trying to use `__DIR__` and it won't even evaluate. The following code: die(`__DIR__`); literally prints "`__DIR__`" to the screen. Does this provide any clues? Also, @Kumar, this is through Apache (the PHP script is serving as a web page); hopefully that helps. – Kim Jul 11 '11 at 18:50
  • `__DIR__` failing? What kind of set up you are using? is it on Windows or Linux? I am not sure about Windows as am on Debian 24x7 – Kumar Jul 12 '11 at 04:05

4 Answers4

15

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.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
1

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

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • thanks for the suggestion and I'll look into it this morning, but I've never had a problem with using "./" or "../" before inside a require_once() statement, and never once had to use the set_include_path() function to correct any issues like this before. Thanks again! – Kim Jul 11 '11 at 11:14
  • IMHO, set include path is not of significance when you are directly including a file as @kim is doing, do correct me if I am wrong. – Kumar Jul 11 '11 at 11:22
  • It could be if someone did a chdir or something like that. – Scott C Wilson Jul 11 '11 at 11:24
0

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.

SEoF
  • 1,092
  • 14
  • 26
-1
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.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • No, thats wrong. `fruit.php` tries to resolve the path using the `include_path` (remind, that the current work directory `.` is usually part of it) and `../fruit.php` tries to resolve it _only_ using the current work directory. – KingCrunch Jul 11 '11 at 12:05
  • Additional: Calling `fruit.php` does not mean, that the current work directory points to the directory, where `fruit.php` is: `php some/directory/fruit.php`. Or it got changed using `chwd()` – KingCrunch Jul 11 '11 at 12:14