473,386 Members | 1,962 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,386 software developers and data experts.

Relative Paths and include files

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 restrictive as
to how I design the file structure when it comes to including files. I
currently have something like below:

folder1
folder2
images
---image1.gif
includes
---header.php
---footer.php
folder3
---folder4
---file0.php
---file1.php
---file2.php
index.php
I am currently thinking I will have to run all my scripts at the same
level to ensure I can include the header.php file in all my pages while
maintaining the integrity of links in the header.php. For example, if I
include header.php in index.php and header.php is referencing
.../images/image1.gif in the images folder, the link will no longer be
valid when included in index.php. Does that make sense?

Can anybody share some tips on what they do to overcome what must be a
very common problem? Is absolute paths the answer so instead of calling
.../images/image.gif from the include file, I would call /images/image1.gif?

Any help that anybody can give me would be much appreciated.

Thanks,
Mark.
Jul 18 '05 #1
7 2816
mark wrote:
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 restrictive as
to how I design the file structure when it comes to including files. I
currently have something like below:

folder1
folder2
images
---image1.gif
includes
---header.php
---footer.php
folder3
---folder4
---file0.php
---file1.php
---file2.php
index.php
I am currently thinking I will have to run all my scripts at the same
level to ensure I can include the header.php file in all my pages while
maintaining the integrity of links in the header.php. For example, if I
include header.php in index.php and header.php is referencing
../images/image1.gif in the images folder, the link will no longer be
valid when included in index.php. Does that make sense?

Can anybody share some tips on what they do to overcome what must be a
very common problem? Is absolute paths the answer so instead of calling
../images/image.gif from the include file, I would call /images/image1.gif?

Any help that anybody can give me would be much appreciated.

Thanks,
Mark.


I generally use absolute pathnames. I seldom use relative ones.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '05 #2
Thanks Jerry, the trouble I have is that my hosting provider sets the
document root to /home so if I was to use absolute paths I would have to
do somethign like

/home/path/to/the/public_html/images/image1.gif

which is a bit long winded. I could store it in a variable but it would
be better if I could set document root to the public_html folder. Do you
know how I can do this with out changing the httpd.conf file?

Thanks.
Jerry Stuckle wrote:
mark wrote:
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 restrictive
as to how I design the file structure when it comes to including
files. I currently have something like below:

folder1
folder2
images
---image1.gif
includes
---header.php
---footer.php
folder3
---folder4
---file0.php
---file1.php
---file2.php
index.php
I am currently thinking I will have to run all my scripts at the same
level to ensure I can include the header.php file in all my pages
while maintaining the integrity of links in the header.php. For
example, if I include header.php in index.php and header.php is
referencing ../images/image1.gif in the images folder, the link will
no longer be valid when included in index.php. Does that make sense?

Can anybody share some tips on what they do to overcome what must be a
very common problem? Is absolute paths the answer so instead of
calling ../images/image.gif from the include file, I would call
/images/image1.gif?

Any help that anybody can give me would be much appreciated.

Thanks,
Mark.

I generally use absolute pathnames. I seldom use relative ones.

Jul 18 '05 #3
mark wrote:
Thanks Jerry, the trouble I have is that my hosting provider sets the
document root to /home so if I was to use absolute paths I would have to
do somethign like

/home/path/to/the/public_html/images/image1.gif

which is a bit long winded. I could store it in a variable but it would
be better if I could set document root to the public_html folder. Do you
know how I can do this with out changing the httpd.conf file?

Thanks.


Mark,

No, you can't change this without access to the httpd.conf file.

However - I'd be VERY surprised if the document root is set to /home. This is
the default directory for your web site; it should NOT be home.

I suspect your real document root is something/public_html (or something
similar). That's what's standard; if it isn't your entire web site would be broken.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '05 #4
ma**@something.com says...
Can anybody share some tips on what they do to overcome what must be a
very common problem? Is absolute paths the answer so instead of calling
../images/image.gif from the include file, I would call /images/image1.gif?


My usual technique is to create a "config" file and populate it with a
number of defined CONSTANTS (note: NOT variables) and then include it in
each of the PHP files in my web application.

Simplified example:

<?php // config file
define('WEB_ROOT', 'http://http_doc_root/');
define('FILE_ROOT', '/absolute_php_root/');
define('IMAGE_WEB_PATH', WEB_ROOT.'images/');
define('IMAGE_FILE_PATH', FILE_ROOT.'images/');
define('INCLUDE_FILE_PATH', FILE_ROOT.'includes/');
define('HEADER_FILE', INCLUDE_FILE_PATH.'header.php');
define('FOOTER_FILE', INCLUDE_FILE_PATH.'footer.php');
?>

<?php // sample app file
//
// first line below is only entry for this file where you might use a full
// path but I usually use a relative one as I will know where this app
// file is in the file system eg. maybe include '../includes/config.php'
// instead
//
include 'includes/config.php';
include HEADER_FILE;
echo 'This is a picture: <br />';
echo '<img src="'.IMAGE_WEB_PATH.'picture.jpg" alt="pic">
include FOOTER_FILE;
?>

Also excellent for portability within or across hosts, simply adjust in
the config file only.

Geoff M
Jul 18 '05 #5
This is what my provider says:

"
There is no easy way to set the document root on the servers as we have
them configured in a certain way which would mean making alot of
changes. The servers use Apache mod_vhost_alias which is why the
document root is /home You could use PHP or Perl to assign a value to a
variable for your document root."

Thanks for everyone's help. I'll check out all the suggestions and
experiment...

Mark.
Jerry Stuckle wrote:
mark wrote:
Thanks Jerry, the trouble I have is that my hosting provider sets the
document root to /home so if I was to use absolute paths I would have
to do somethign like

/home/path/to/the/public_html/images/image1.gif

which is a bit long winded. I could store it in a variable but it
would be better if I could set document root to the public_html
folder. Do you know how I can do this with out changing the httpd.conf
file?

Thanks.


Mark,

No, you can't change this without access to the httpd.conf file.

However - I'd be VERY surprised if the document root is set to /home.
This is the default directory for your web site; it should NOT be home.

I suspect your real document root is something/public_html (or something
similar). That's what's standard; if it isn't your entire web site
would be broken.

Jul 18 '05 #6

"mark" <ma**@something.com> wrote in message
news:db*********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
Thanks Jerry, the trouble I have is that my hosting provider sets the
document root to /home so if I was to use absolute paths I would have to
do somethign like

/home/path/to/the/public_html/images/image1.gif

which is a bit long winded. I could store it in a variable but it would be
better if I could set document root to the public_html folder. Do you know
how I can do this with out changing the httpd.conf file?


You don't need to hard code the name of your root folder as it is available
in $_SERVER['DOCUMENT_ROOT']. Check it out at
http://uk2.php.net/reserved.variables

--
Tony Marston

http://www.tonymarston.net

Jul 18 '05 #7
mark wrote:
This is what my provider says:

"
There is no easy way to set the document root on the servers as we have
them configured in a certain way which would mean making alot of
changes. The servers use Apache mod_vhost_alias which is why the
document root is /home You could use PHP or Perl to assign a value to a
variable for your document root."

Thanks for everyone's help. I'll check out all the suggestions and
experiment...

Mark.
Jerry Stuckle wrote:
mark wrote:
Thanks Jerry, the trouble I have is that my hosting provider sets the
document root to /home so if I was to use absolute paths I would have
to do somethign like

/home/path/to/the/public_html/images/image1.gif

which is a bit long winded. I could store it in a variable but it
would be better if I could set document root to the public_html
folder. Do you know how I can do this with out changing the
httpd.conf file?

Thanks.


Mark,

No, you can't change this without access to the httpd.conf file.

However - I'd be VERY surprised if the document root is set to /home.
This is the default directory for your web site; it should NOT be home.

I suspect your real document root is something/public_html (or
something similar). That's what's standard; if it isn't your entire
web site would be broken.


If they told me that, I'd be looking for a new hosting company immediately (if
not sooner).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '05 #8

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

Similar topics

7
by: Doug | last post by:
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...
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...
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...
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...
19
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other...
6
by: Max | last post by:
Hello - Just switched to PHP 5 and just realized that all my scripts with relative paths for require statements no longer work unless I put the absolute path. Usually, if the file is in the same...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.