problem with 'required' from a different directory?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

problem with 'required' from a different directory?

Post by phpBever »

I've got a file we'll call documentroot/affiliate/file1.php. It requires and includes a number of other files in the affiliate directory.

When I run it by itself it runs fine (eg http://www.documentroot/affiliate/file1.php).

When I require it in documentroot/file2.php I get two kinds of errors:
--permission failure with a fwrite function
--"Call to undefined function sqlconnect()" fatal error in one of the files included in file1.php, we'll call "documentroot/affiliate/lib/file4.php".

All the permissions seem the same in documentroot and in documentroot/affiliate. And the database information at the beginning of file4.php all looks right.

Is there some principle about require and include from other directories that I'm missing? Or some other reason these kinds of problems could be arising when I require file1 from within file 2?

Thanks.
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: problem with 'required' from a different directory?

Post by Gargoyle »

all require paths need to be absolute or based upon the path of the php file that has been called.
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: problem with 'required' from a different directory?

Post by phpBever »

I'm requiring documentroot/affiliate/file1.php from documentroot/file2.php with

Code: Select all

require "./affiliate/file1.php";
Doesn't that meet the requirement?

If it didn't, wouldn't the problem be one of not finding the right file rather than of the kinds of errors I'm seeing, where the required file is running but there are permission and "undefinedfunction sqlconnect()" errors?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with 'required' from a different directory?

Post by requinix »

The path resolution for include/require is wonky. Stick with absolute paths.

Code: Select all

// relative to the website root
include $_SERVER["DOCUMENT_ROOT"] . "/affiliate/file1.php";

// relative to the current directory
include __DIR__ . "/affiliate/file1.php"; // PHP 5.3+
include dirname(__FILE__) . "/affiliate/file1.php"; // PHP <5.3
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: problem with 'required' from a different directory?

Post by phpBever »

Thanks, Tasairis. I think you're saying that each of those examples count as using the absolute path. Is that right? That would be a way around the disadvantage of hardcoding with the actual domain name (http://www.theDomain.com/affiliate/file1.php)?
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: problem with 'required' from a different directory?

Post by phpBever »

This is strange--I don't understand what's happening.

I'm telling my file to include

Code: Select all

require $_SERVER["DOCUMENT_ROOT"]."/affiliate/sale.php"
Apache is giving me this error:
PHP Warning: require(/var/www/htmlsale.php) [<a href='function.require'>function.require</a>]: failed to open stream: No such file or directory in /var/www/html/affiliate/ericsPlugin.php on line 26
PHP Fatal error: require() [<a href='function.require'>function.require</a>]: Failed opening required '/var/www/htmlsale.php' (include_path='.:') in /var/www/html/affiliate/ericsPlugin.php on line 26
How is Apache getting from
$_SERVER["DOCUMENT_ROOT"]."/affiliate/sale.php"
to
/var/www/htmlsale.php
??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with 'required' from a different directory?

Post by requinix »

Pay closer attention to the error message:
No such file or directory in /var/www/html/affiliate/ericsPlugin.php on line 26
Fix that one.
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: problem with 'required' from a different directory?

Post by phpBever »

I don't get it.

I understood from what you said earlier that this was a way to indicate an absolute path--at any rate I proposed that interpretation and noone said it was wrong when I wrote
phpBever wrote:I think you're saying that each of those examples count as using the absolute path. Is that right
tasairis wrote:include $_SERVER["DOCUMENT_ROOT"] . "/affiliate/file1.php"
My line 26 just follows that model. So I don't know what the error message is telling me to fix or what explains the strange interpretation apache is giving to the expression.

According to testPHP.php, the document_root is /var/www/html.

As I understand things, this expression

Code: Select all

require $_SERVER["DOCUMENT_ROOT"]."/affiliate/sale.php"
should be equivalent to

Code: Select all

require "/var/www/html/affiliate/sale.php"
which should be equivalent, without hardcoding in this particular domain name, to

Code: Select all

require "http://www.domain.com/affiliate/sale.php"
Maybe I've got it all wrong, but then please tell me and don't just throw out obscure hints.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem with 'required' from a different directory?

Post by requinix »

You may have fixed that one particular include() but the error message is indicating a different one that also needs fixing.
phpBever wrote:As I understand things, this expression

Code: Select all

require $_SERVER["DOCUMENT_ROOT"]."/affiliate/sale.php"
should be equivalent to

Code: Select all

require "/var/www/html/affiliate/sale.php"
which should be equivalent, without hardcoding in this particular domain name, to

Code: Select all

require "http://www.domain.com/affiliate/sale.php"
Maybe I've got it all wrong, but then please tell me and don't just throw out obscure hints.
The first two, yes, but the first has the advantage of working regardless of where the document root is (the second has it hardcoded and has to change everywhere if the files ever move).
What you're getting at with the third is correct, but the code itself is not.

Yes, /var/www/html/affiliate/sale.php is the same file as used in http://www.domain.com/affiliate/sale.php. But there's a big difference between the two: the former is a file and the latter is the output of the file. If you include() the first one then it works because you're including a file with PHP code inside, but if you try to include() the latter you'll be running the output of the script.

For 99% of cases you do not want to include() or require() anything over the Internet - ie, any "http://" location.
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: problem with 'required' from a different directory?

Post by phpBever »

I think I understand the bit about getting a file vs getting the output of a file, although I think I've seen the "http://..." url used in include calls.

What I still don't understand--and I'm giving up and trying a different approach--is why when I require FileB in FileA, this line works:

Code: Select all

require $_SERVER["DOCUMENT_ROOT"]."FileB.php"
But the same logical structure doesn't work when I reach "require FileC" towards the end of FileB:

Code: Select all

require $_SERVER["DOCUMENT_ROOT"]."/affiliate/FileC.php"
Apache rather interprets the path in the wonky way we saw earlier ('/var/www/htmlsale.php), where 'sale' is FileC.
(That's the only require/include line in FileB.)

Furthermore, if I run FileB independently (rather than required in FileA), FileC and its "includes" work fine.
Initially the require line in FileB was just

Code: Select all

require "FileB.php";
This approach created the two kinds of problems I mentioned in my first post: permission problems opening a file for fwrite and this error: "Call to undefined function sqlconnect()" fatal error in FileD" (which is included in FileC).

Based on the reply I got to my first post, I have since been trying to make it work using absolute paths. But that only seems to have made the problem worse.

I've actually created a simple model (set of 2-3 line files spread over the various relevant directories) where the absolute path using $_SERVER['DOCUMENT_ROOT'] does work right. So I'm down to wondering if it's something about FileC that's causing the strange interpretation of the require call. But if so, what could it be? And how could something in FileC affect how Apache interprets the require command in FileB?

(Note that FileC is part of someone else's application, to which I'm trying to create a different entree using FileA and FileB. I don't really understand everything in FileC and its "includes", so if there is the theoretical possibility that it's something in FileC that's causing the problem, I'll just give up.
Post Reply