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

Started by
7 comments, last by deadimp 16 years, 10 months ago
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
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
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.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
https://svn.limb-project.com/limb/3.x/trunk/limb/core/common.inc.php
Thanks for the info!
I'm not sure yet if I'll incorporate this, for I'm unsure how important this feature will be...
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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.
Yeah, that's what I've doing so far. Though, now I'm beginning to reconsider Sander's point... I'm not sure I'll change everything to absolute paths, but already I've switch a few over.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement