473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

include() and path resolution

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
5 2123
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
"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: Lüpher Cypher | last post by:
Hi, I just want to make sure I'm getting it right: When I used nested include/include_once, I noticed that, say: /a.php -> include("path/b.php"); /path/b.php -> include("c.php"); /path/c.php...
2
by: Marcus | last post by:
Hello, I am having problems with an include statement. I'm setting a session variable flag and then including a file, and in that include file I have a check at the top to make sure that the...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
1
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not...
13
by: Adam | last post by:
Trying to get an asp.net 2.0 app running and am receiving this error. I see a bunch of people with this error on the net, but no solution: Works fine on my local machine, deployed to a server it...
2
by: Nick Craig-Wood | last post by:
We are currently investigating whether to move the data files from our application into python for ease of maintenance. Each data item turns into a class definition with some class data. The...
9
by: larrybud2002 | last post by:
I've read about this error in this group and others from early 2006 but without any resolution, so I thought I'd bump it up to see if there's a solution. Trying to build a website on ...
8
by: moondaddy | last post by:
I'm posting code for a user control ( FunctionConnectorSelector) below which has 3 content controls in it. each content control uses a style from a resource dictionary merged into the app.xaml...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.