By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

include() and path resolution

P: n/a
Hi All,

I have a PHP program that includes files from the 'include' directory
relative to the path that translates to the HTTP root of the site.
This is '/var/www/html' on the server, and '~/username/public_html' on
my dev box.

My question is - is there any way to resolve the path to a file,
something like:

include GetBasePath() . '/include/top.inc';

where GetBasePath() returns '/var/www/html' or
'~/username/public_html' depending upon which server it's running?
I've looked through the manual & the FAQ but found nothing.

TIA for any suggestions.

Yours,
Duncan Bayne
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Duncan Bayne wrote:
I have a PHP program that includes files from the 'include' directory
relative to the path that translates to the HTTP root of the site.
This is '/var/www/html' on the server, and '~/username/public_html' on
my dev box.

My question is - is there any way to resolve the path to a file,
something like:

include GetBasePath() . '/include/top.inc';

where GetBasePath() returns '/var/www/html' or
'~/username/public_html' depending upon which server it's running?
I've looked through the manual & the FAQ but found nothing.


You could try using
dirname(__FILE__)
That will give you the absolute path to the directory of the file you call
it in.

If you wanted to include the file '../somedir/somefile.php' relative to the
current file you could do
include(dirname(__FILE__) . '../somedir/somefile.php')

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
"Duncan Bayne" <dh*****@ihug.co.nz> wrote in message
news:f8*************************@posting.google.co m...
Hi All,

I have a PHP program that includes files from the 'include' directory
relative to the path that translates to the HTTP root of the site.
This is '/var/www/html' on the server, and '~/username/public_html' on
my dev box.

My question is - is there any way to resolve the path to a file,
something like:

include GetBasePath() . '/include/top.inc';

where GetBasePath() returns '/var/www/html' or
'~/username/public_html' depending upon which server it's running?
I've looked through the manual & the FAQ but found nothing.


include($_SERVER['DOCUMENT_ROOT'] . '/include/top.inc');

- Virgil
Jul 17 '05 #3

P: n/a
Why do you think you need such a thing? The include_path option in the
PHP.INI file may contain several entries, therefore it may not be possible
to resolve it to a single path. Besides, PHP will automatically scan ALL the
entries in the include_path when attempting to resolve an include()
statement. You do NOT need to specify any directory name on an include()
statement as PHP will examine all the directory names on the include_path
setting.

--
Tony Marston

http://www.tonymarston.net

"Virgil Green" <vj*@obsydian.com> wrote in message
news:R8***************@newssvr23.news.prodigy.com. ..
"Duncan Bayne" <dh*****@ihug.co.nz> wrote in message
news:f8*************************@posting.google.co m...
Hi All,

I have a PHP program that includes files from the 'include' directory
relative to the path that translates to the HTTP root of the site.
This is '/var/www/html' on the server, and '~/username/public_html' on
my dev box.

My question is - is there any way to resolve the path to a file,
something like:

include GetBasePath() . '/include/top.inc';

where GetBasePath() returns '/var/www/html' or
'~/username/public_html' depending upon which server it's running?
I've looked through the manual & the FAQ but found nothing.


include($_SERVER['DOCUMENT_ROOT'] . '/include/top.inc');

- Virgil

Jul 17 '05 #4

P: n/a
Tony Marston wrote:
Why do you think you need such a thing? The include_path option in the
PHP.INI file may contain several entries, therefore it may not be possible
to resolve it to a single path. Besides, PHP will automatically scan ALL
the entries in the include_path when attempting to resolve an include()
statement. You do NOT need to specify any directory name on an include()
statement as PHP will examine all the directory names on the include_path
setting.


That's all well and good saying that, but if you are hosting on a shared
hosting platform and cannot alter the php.ini to your specific directory of
includes then your advice is not much help.

Note however that you can alter the include path in an .htaccess file,
assuming you are on an Apache host on a Unix platform, or can use ini_set()
to change the include path at the beginning of your scripts and then just
call include() without a path as Tony here suggests.

If you do have to use ini_set() then you'll need to check the other posts
about how to set it to the absolute path which comes back to your original
question. (BTW I like Virgil's way better than I suggested!)
Jul 17 '05 #5

P: n/a

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:c8*******************@news.demon.co.uk...
"Virgil Green" <vj*@obsydian.com> wrote in message
news:R8***************@newssvr23.news.prodigy.com. ..
"Duncan Bayne" <dh*****@ihug.co.nz> wrote in message
news:f8*************************@posting.google.co m...
Hi All,

I have a PHP program that includes files from the 'include' directory
relative to the path that translates to the HTTP root of the site.
This is '/var/www/html' on the server, and '~/username/public_html' on
my dev box.

My question is - is there any way to resolve the path to a file,
something like:

include GetBasePath() . '/include/top.inc';

where GetBasePath() returns '/var/www/html' or
'~/username/public_html' depending upon which server it's running?
I've looked through the manual & the FAQ but found nothing.

include($_SERVER['DOCUMENT_ROOT'] . '/include/top.inc');

- Virgil

Why do you think you need such a thing? The include_path option in the
PHP.INI file may contain several entries, therefore it may not be possible
to resolve it to a single path. Besides, PHP will automatically scan ALL

the entries in the include_path when attempting to resolve an include()
statement. You do NOT need to specify any directory name on an include()
statement as PHP will examine all the directory names on the include_path
setting.


(top-posting 'corrected' for my reading pleasure)

I don't. The OP might. In addition to the include_path, the include will
look in the same directory as the currently executing file.

Personally, I prefer to have just the . in the include_path along with any
other system-level includes I might need and then use relative filenames.
But, as Chris pointed out, you might not always have access to the php.ini.
You might not even be able to change include_path via .htaccess if your host
has implemented phpsuexec (or phpsu, or whatever variant). Then, you are
supposed to use a private php.ini... but I haven't tried or bothered. I
still prefer relative file paths.

- Virgil
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.