Add script's parent directory to include_path

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Add script's parent directory to include_path

Post by rmccue »

One more PHP question:
How do I add the parent directory ( ../ ) to the include path?
I have tried using

Code: Select all

ini_set('include_path',ini_get('include_path').';..;');
set_include_path('..' . PATH_SEPARATOR . get_include_path() );
but in the file in the parent directory it uses

Code: Select all

require_once './conf.php';
and the script fails on this.
Thanks
Last edited by rmccue on Wed Oct 11, 2006 2:59 am, edited 1 time in total.
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Post by rmccue »

Weirdan wrote: You may bump it since it's about 2 days old.
In that case, Bump :D
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

In general, using relative paths in the include path is not a good idea. You probably would be better off hard-coding the path. The double-dot notation should work, but since it doesn't seem to in your case, you can try:

Code: Select all

set_include_path(realpath('..') . PATH_SEPARATOR . get_include_path() );
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Post by rmccue »

I am trying to avoid hard-coding paths, because it's just one more thing that can go wrong ;)
That doesn't work either :(
Should I try getting rid of the . in the include paths?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I think I might need more information. Can you give me your directory structure? Where is conf.php located?
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Post by rmccue »

The directory structure is like this:

/conf.php
/lib.php
/admin/
/admin/index.php
/inc/magpie/rss_parse.php

I am including lib.php from admin/index.php, which includes ./conf.php
lib.php also includes ./inc/magpie/rss_parse.php (in case having more in other directories matters)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

In your file that is below root (admin/whatever.php) add this line to the beginning of the file...

Code: Select all

<?php
$path_to_root= './../';
?>
Then, whatever you want to include in that script that is in the root folder can be called like:

Code: Select all

<?php
include $path_to_root . 'filename.php';
?>
Of course, you could always build a realpath and use that throughout your script so it makes no difference what your files are. Or, you can process includes once in one spot, and not worry about them throughout the script.
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Post by rmccue »

That's what I have resorted to for now.
However, if there are any other options, that would probably be easier.
Post Reply