Sign in to follow this  
deadimp

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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this