Jump to content

includes not working consistently on different servers


vampke

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.

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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.