Forum Moderators: coopster

Message Too Old, No Replies

Relative Paths in require once()

Relative to What?

         

cmarshall

5:03 pm on Dec 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an interesting thing.

I just set up a new VPS. I gave it Apache 2.2 and PHP 5.6.1.

I moved a site over that had been running for some months on a PHP 5.1.2 server, and immediately experienced failures.

This was because an included file was being referenced from the running PHP file, not the included file.

To wit:

With this structure:
/index.php

which includes:

/wp-content/plugins/my_plugin/my_plugin.php

which includes:

/wp-content/plugins/my_plugin/include_me.php

if(file_exists('wp-content/plugins/my_plugin/include_me.php')
{
require_once('wp-content/plugins/my_plugin/include_me.php');
}

Don't worry about the prior test. There's a reason for it.

In any case, this code is inside a PHP file (/wp-content/plugins/my_plugin/my_plugin.php) that has been included into another file that is running at the top level (typical WordPress structure), so the path of the file in which this code appears is relative to the top level.

Now, when I moved it onto the new server, I had to change it; thusly:

if(file_exists('wp-content/plugins/my_plugin/include_me.php')
{
require_once('include_me.php');
}

Now, THAT is wierd. Note that the file_exists() still checks from the top level, but the require_once() is relative to the include.

I suspect that I have neglected to set a flag in my PHP compile.

Any ideas?

cmarshall

7:25 pm on Dec 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just as an addendum.

Here [bugs.php.net] is a bug report about this exact same issue on php.net [php.net] that is marked as "Bogus, dude"; without any explanation. Helpful folks, over at PHP.

So, it's "bogus," but why?

cmarshall

11:16 pm on Dec 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello?

TAP

TAP

TAP

Is dis ting on?

I'm digging through piles of mush, trying to figure the answer out. I assume that it's very basic, but someone here must have some kind of answer.

eelixduppy

5:31 am on Dec 3, 2007 (gmt 0)



Do you have the include path directive set to anything in your php.ini configuration file?

RonPK

9:50 am on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for the record: the bug report is about an issue with including files with names starting with './' or '../'. Such names are treated differently by PHP, according to the manual [php.net]: "If filename begins with ./ or ../, it is looked only in the current working directory." (my emphasis, their English).

cmarshall

11:18 am on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the responses. I don't mess with the working directory at all. This code works on many installations of PHP, so there is definitely something up with my compile or php.ini.I've never encountered it before. However, it seems to be very common [google.com].

I'm pretty sure I'll get it figured out today. One of my employees is a PHP expert. I'll ask him.

henry0

12:43 pm on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you are using a "pre-made" app
my guess is that even if it worked before, the new install looks for a path not similar to the previous.

Try a few path modifications out of your application
config file or any file related to its "config"

psiron

4:21 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



Hi,

I use one config file and absolute paths inside the config file like this:

#####################
# INSTALLED MODULES #
#####################

// NOTEPAD MODULE
require_once($_SERVER["DOCUMENT_ROOT"].'/extranet/modules/notepad/config.php');

It works fine for me but Im sure others will have some thoughts.

cmarshall

5:40 pm on Dec 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I solved this (still not exactly sure how) by doing this:

require_once([red][b]dir_name(__FILE___).[/b][/red]'include_me.php'_);

This makes an absolute path.