• 9
• 16
• 15
• 12
• 9

# [web] [php] Include path, depending on ref to parent directory '..'

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've just noticed something in PHP - I'm unable to access an 'intended' file in the search directories (include path) when referencing to them via '..' in the normal include. However, if I simply concatenate the determined search path (the one where I know the file resides) it will work - even with the redundant '..' path. Example:
File Setup:
/
cur/
index.php (/cur/index.php)
test/
test.php (/test/test.php)
sub/ (/test/sub)

[[ /cur/index.php ]]
$path="../test/sub";$inc="../test.php";
set_include_path($path); echo "$path/$inc<br>"; //Works: include("$path/$inc"); //Does not work: include("$inc");

[[ /test/test.php ]]
echo "Test";

[[ /cur/index.php: Output - When it works ]]
../test/sub/../test.php
Test

On error, it says that it can't find the file, and prints out the include path as I set it


I know that I can manually set the path, or manually check the files, but I'd rather use basic PHP to acheive this task. If you need an explanation as to why I'm doing this, I'll explain. PHP 5.2.1 Windows XP SP2

##### Share on other sites
The PHP documentation for include mentions that:

Quote:
 If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.

##### Share on other sites
I wouldn't use the include_path thing since you will get confused if you got more paths. Just manually setting them will do :)

##### Share on other sites
I do everything with absolute paths. Then it doesn't matter from where you execute the script (I also write CLI applications in PHP). To get the absolute path of the file you're in:

$path = dirname(__FILE__) . '/'; Then I just do: require_once($path . 'test/test.php');
require_once(\$path . 'test/sub/whatever.php');

etcetera.

##### Share on other sites
I want to be able to use the include paths to allow myself (and maybe others) to override the 'default' script (the one in the base, external directory) with a 'contemporary' one, using the automatic and ordered file detection of the incude directive.

[Finish post later]

##### Share on other sites
https://svn.limb-project.com/limb/3.x/trunk/limb/core/common.inc.php

##### Share on other sites
Thanks for the info!
I'm not sure yet if I'll incorporate this, for I'm unsure how important this feature will be...

##### Share on other sites
I normally set the php include path to include my "includes" directory, then include all the files from there, so I might have "classes/someClass.cls" but really it's looking in "root/includes/classes/" to find it. Whenever the script changes environments, I just change the include path in one place.