Jump to content

Relative paths and 'include'


mrtndimitrov

Recommended Posts

Recently I discovered the following strange behavior of 'include' on PHP 5.2 (Windows). According to the documentation and to my understanding 'include' is supposed to simply 'copy' the contents of the file into the calling one. So if we put another 'include' into the included file, the path should be relative to the root file. If you do 'echo getcwd();' from any of the files, the path is indeed relative to the root one. But I discovered that if PHP can not find the file, it searches by making the working directory the included file. Here is the directory structure I set up to test the things:

test/index.php              {contains: include 'func/main.inc.php';}

test/func/main.inc.php    {contains: include '1.inc.php'; include 'func/2.inc.php'; include '../func/1.inc.php';

test/func/1.inc.php        {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';

test/func/2.inc.php        {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';

test/func/func/2.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';

 

The results are:

the first include in main.inc.php has no problem finding 1.inc.php

the second include in main.inc.php includes test/func/2.inc.php NOT test/func/func/2.inc.php which means PHP is first trying to locate the file based on index.php's working directory

the third include prints warnings as it will include './2.inc.php'

 

Is this behavior documented? Can we rely on it?

 

martin

Link to comment
Share on other sites

Of course it is documented (though it is less clear in the current description than previous ones) - http://us2.php.net/manual/en/function.include.php

 

An absolute file system path or a relative path using ./ or ../ will use that absolute file system path or that path relative to the main parent file. If you just specify a file name, the include_path is searched to find the file. A dot . in the include_path refers to the current directory, so by having the dot . be first in the include_path, the current directory is always searched first.

 

You may want to consider using $_SERVER['DOCUMENT_ROOT'] to form absolute file system paths -

 

include $_SERVER['DOCUMENT_ROOT'] . '/path_to_file/filename.ext';

Link to comment
Share on other sites

Of course it is documented (though it is less clear in the current description than previous ones) - http://us2.php.net/manual/en/function.include.php

 

Sorry but I don't see in the documentation description of such behavior. When in the included file I print the current working directory, it is the parent's one. So, why include '2.inc.php'; is resolved?

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.