Jump to content
  • Advertisement
Sign in to follow this  
deadimp

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

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

If you intended to correct an error in the post then please contact us.

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
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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!