Jump to content

includes not working consistently on different servers


Recommended Posts

Hi peepz,

 

I am writing a script that seems to be doing what I mean it to except for one thing.

I have a file in a subdirectory (uri/includes) which I include in my php files of the parent directory (uri):

 

include 'firstfile.inc.php';

 

The firstfile.inc.php file includes another file in the same includes directory.

 

include 'secondfile.inc.php';

On my local windows machine this works fine. On my linux webserver not so much: in stead of including the file in 'uri/includes/secondfile.inc.php' it tries to include 'uri/secondfile.inc.php'

 

Could anyone be so kind to explain why this is happening (a server setting I need to be looking at?) and how to overrun this setting in the code? I am on a shared hosting account with no access to php.ini.

 

Cheers.

 

v.

Anecdotally, the rules for finding pathless files with include() has varied over time and PHP versions: sometimes it might search the directory of the parent file, sometimes it only looks at the working directory.

 

The best thing to do is be very explicit with your filenames by using absolute paths. For something relative to the directory of the parent file you can

include __DIR__ . '/firstfile.inc.php'; // PHP 5.3+
include dirname(__FILE__) . '/firstfile.inc.php';

and for something relative to the root of your website,

include $_SERVER['DOCUMENT_ROOT'] . '/includes/firstfile.inc.php';

thanks for your reply requinix. I was not aware of this.

It seems like very poor consistency in php...

I already came up with a solution similar to yours, however I was looking for a reason for the incongruity. Very surprised to find it out.

This also depends upon the host's configuration, mind you.

Most hosts I've seen, however, will at least allow for the include to be relative to the main file. The same with most PHP versions I've encountered, at least the last few (7+) years.

Also the documentation for include() explains the logic used.

I understand the language evolves, but there really is no logical explanation to changing these kind of things is there?

Unless the change was for the better.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.