473,390 Members | 865 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,390 software developers and data experts.

relative include paths? What's the use?


If I were to write an include with a relative path like

include("../conf/config.php");

What is the use?

As far as I understand it, the path is relative to the first script that
is called by php.

In other words, if the current working directory is /www/ and you were
"executing" a.php in that directory, then a.php included
/www/include/b.php by doing include("include/b.php");, then b.php tried
to include file /www/config/c.php, by doing include("../config/c.php) it
would not work. Instead php would be looking in /config for c.php and
it would not find it (?!).

Relative paths in include directives seem useless, because the paths are
not relative to the file that the include directive is in, like you
would expect them to be. These includes are totally dependant on where
the original file was "run" from.

Can any one shed some light on why relative paths in includes are of
-any- practical use in PHP? Or, is this a bug?

PHP 4.3.4 (cli) (built: Mar 9 2004 11:40:14)

By the way, I am just trying to separate my php project files into a few
directories. I just want to be able to include them without using
absolute paths. These files in these different directories should be
able to include each other.

-d

Jul 17 '05 #1
7 2764
"Doug" <do*****@XXXearthlinkXXX.net> wrote in message
news:rs*****************@newsread2.news.atl.earthl ink.net...

If I were to write an include with a relative path like

include("../conf/config.php");

What is the use?

As far as I understand it, the path is relative to the first script that
is called by php.


That's because in PHP, an include is an runtime operation and not a
preprocess macro expansion. As an operation, it's perfectly logical that
relative paths are relative to the runtime path and not the source path.
This is certainly not a bug.

To do multiple level relative includes, you can use __FILE__ to determine
the location of the current file, and append the directory path in the
include statement. Example:

<?

$CURRENT_SOURCE_FOLDER = dirname(__FILE__);

require_once("$CURRENT_SOURCE_FOLDER/grumpy.php");
require_once("$CURRENT_SOURCE_FOLDER/sleepy.php");
require_once("$CURRENT_SOURCE_FOLDER/sneezy.php");

....
Jul 17 '05 #2
In article <rs*****************@newsread2.news.atl.earthlink. net>,
Doug <do*****@XXXearthlinkXXX.net> wrote:
Can any one shed some light on why relative paths in includes are of
-any- practical use in PHP? Or, is this a bug?


I use them in the following way:

The first file included in every script is a small config file (in the
same directory as the script itself, so it's guaranteed to be found in a
default PHP setup) which sets the include paths. In that file I have
something like this:

ini_set('include_path', ini_get('include_path') . ':' .
realpath('relative/path/to/include_directory'));

Because of the 'realpath' function, which translates relative paths to
absolute paths, I can copy the directory tree of my application to
another machine and everything will keep working, even if the directory
structure 'above' my application is different.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #3
Ok, but how often do you find that even though you don't use relative paths,
your sites doesn't work? Even if the top structure is different from your
development PC than your server / ISP server? I haven't had to use it yet.
In ASP yes, but not really in PHP

--

Kind Regards
Rudi Ahlers
+27 (82) 926 1689

Greater love has no one than this, that he lay down his life for his friends
(John 15:13).
"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <rs*****************@newsread2.news.atl.earthlink. net>,
Doug <do*****@XXXearthlinkXXX.net> wrote:
Can any one shed some light on why relative paths in includes are of
-any- practical use in PHP? Or, is this a bug?


I use them in the following way:

The first file included in every script is a small config file (in the
same directory as the script itself, so it's guaranteed to be found in a
default PHP setup) which sets the include paths. In that file I have
something like this:

ini_set('include_path', ini_get('include_path') . ':' .
realpath('relative/path/to/include_directory'));

Because of the 'realpath' function, which translates relative paths to
absolute paths, I can copy the directory tree of my application to
another machine and everything will keep working, even if the directory
structure 'above' my application is different.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #4


Chung Leong wrote:
"Doug" <do*****@XXXearthlinkXXX.net> wrote in message
news:rs*****************@newsread2.news.atl.earthl ink.net...
If I were to write an include with a relative path like

include("../conf/config.php");

What is the use?

As far as I understand it, the path is relative to the first script that
is called by php.

That's because in PHP, an include is an runtime operation and not a
preprocess macro expansion. As an operation, it's perfectly logical that
relative paths are relative to the runtime path and not the source path.
This is certainly not a bug.

To do multiple level relative includes, you can use __FILE__ to determine
the location of the current file, and append the directory path in the
include statement. Example:

<?

$CURRENT_SOURCE_FOLDER = dirname(__FILE__);

require_once("$CURRENT_SOURCE_FOLDER/grumpy.php");
require_once("$CURRENT_SOURCE_FOLDER/sleepy.php");
require_once("$CURRENT_SOURCE_FOLDER/sneezy.php");


Regardless of the implementatation details (runtime vs. preprocess),
they could've done it either way. Just like the makers of gcc could
have chose to do it either way. The PHP programmers chose to make the
path relative to the first script that is called by php.

In your example, you changed the paths to be absolute. That makes it so
that the file included would be the file that most people expect will be
included in the first place.

Could some one give me a practical example of a -relative- path being
used in PHP?
-d

Jul 17 '05 #5
In article <r8********************@is.co.za>,
"Rudi Ahlers" <SP4M_Rudi@SP4M_Bonzai.org.za_SP4M> wrote:
Ok, but how often do you find that even though you don't use relative paths,
your sites doesn't work? Even if the top structure is different from your
development PC than your server / ISP server? I haven't had to use it yet.
In ASP yes, but not really in PHP


Well, on my development machine I have my sites in my home folder
(~/Sites on Mac OS X) and on the server (also Mac OS X) they are in
/Library/WebServer/Documents.

It was also very convenient when we went from Linux (webserver in
/usr/local/httpd/htdocs) to Mac OS X -- I could simply copy my sites
without breaking anything.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #6
"Doug" <do*****@XXXearthlinkXXX.net> wrote in message
news:HD*****************@newsread3.news.atl.earthl ink.net...
Regardless of the implementatation details (runtime vs. preprocess),
they could've done it either way. Just like the makers of gcc could
have chose to do it either way. The PHP programmers chose to make the
path relative to the first script that is called by php.


The fact that it's an runtime operation makes a big difference. Suppose I
write a function that include a file:

function CustomInclude($path) {
include($path);
echo "<!-- including $path -->";
}

And suppose I place this function in /chung/utilities/useless/functions.php.
When I pass a relative path to this function in some project, I certainly
wouldn't want the path to be relative to the file containing the function.
Jul 17 '05 #7


Chung Leong wrote:
"Doug" <do*****@XXXearthlinkXXX.net> wrote in message
news:HD*****************@newsread3.news.atl.earthl ink.net...
Regardless of the implementatation details (runtime vs. preprocess),
they could've done it either way. Just like the makers of gcc could
have chose to do it either way. The PHP programmers chose to make the
path relative to the first script that is called by php.

The fact that it's an runtime operation makes a big difference. Suppose I
write a function that include a file:

function CustomInclude($path) {
include($path);
echo "<!-- including $path -->";
}

And suppose I place this function in /chung/utilities/useless/functions.php.
When I pass a relative path to this function in some project, I certainly
wouldn't want the path to be relative to the file containing the function.


good point.

-d

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mark | last post by:
I am designing a website at the moment and looking at the difference between relative and absolute url links which is driving me crazy! I would like to use relative paths, but it is proving very...
5
by: jason | last post by:
Can anyone help me find a solution to quickly working out relative paths to a folder in the root of my server... Although it easy when you go - say - two levels down: .../includes it...
7
by: jason | last post by:
I am getting twisted by the possibility that my virtual includes which currently work great on non-domain remote IP will crash if I purchase a domain and point it to one of my designated...
15
by: Nick K. | last post by:
I recently began maintenance work on a production web server that is located in the root directory of a web server. I moved this into a sub web on my local web server in order to do work on it. I...
7
by: Rizaan Jappie | last post by:
is it possible to get the relative path based on a absolute path in c#? *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
0
by: Jason Lawrence | last post by:
I have an attributed ATL project (call it B) that I am building with Microsoft Visual C++ .NET (55537-640-3684941- 18356). In the project I include the COM generated file A.h (from another ATL...
3
by: Andreas Busse | last post by:
Hi, I received a big fat package of source code developed with Metrowerks CodeWarrior and now I am trying to convert this project to VC.NET 2003. My problem is, that in order to compile I have...
2
by: Joe | last post by:
Hi, can someone tell me how to set up relativ paths in VS2003 C++ ? I have some source with a tree directory structure that segments include files in various directories: #include...
11
by: MBS | last post by:
I am playing around with some PHP code. I want to put in an include() function so I can include existing HTML code and output it to the browser. Lo and behold PHP does not support relative paths...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.