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
[web] [php] Include path, depending on ref to parent directory '..'
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:
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
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.
I wouldn't use the include_path thing since you will get confused if you got more paths. Just manually setting them will do :)
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.
$path = dirname(__FILE__) . '/';
Then I just do:
require_once($path . 'test/test.php');
require_once($path . 'test/sub/whatever.php');
etcetera.
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]
[Finish post later]
Thanks for the info!
I'm not sure yet if I'll incorporate this, for I'm unsure how important this feature will be...
I'm not sure yet if I'll incorporate this, for I'm unsure how important this feature will be...
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement