471,310 Members | 1,091 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 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 2666
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Doug | last post: by
1 post views Thread by Alex VanderWoude | last post: by
7 posts views Thread by Rizaan Jappie | last post: by
2 posts views Thread by Joe | last post: by
11 posts views Thread by MBS | last post: by
19 posts views Thread by Jerry M. Gartner | last post: by
6 posts views Thread by Max | last post: by

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.