Connecting Tech Pros Worldwide Forums | Help | Site Map

Relative Paths and include files

mark
Guest
 
Posts: n/a
#1: Jul 18 '05
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.

Jerry Stuckle
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Relative Paths and include files


mark wrote:[color=blue]
> 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.[/color]

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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
mark
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Relative Paths and include files


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:[color=blue]
> mark wrote:
>[color=green]
>> 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.[/color]
>
>
> I generally use absolute pathnames. I seldom use relative ones.
>[/color]
Jerry Stuckle
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Relative Paths and include files


mark wrote:[color=blue]
> 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.
>
>[/color]

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.
jstucklex@attglobal.net
==================
Geoff Muldoon
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Relative Paths and include files


mark@something.com says...
[color=blue]
> 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?[/color]

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
mark
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Relative Paths and include files


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:[color=blue]
> mark wrote:
>[color=green]
>> 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.
>>
>>[/color]
>
> 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.
>
>[/color]
Tony Marston
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Relative Paths and include files



"mark" <mark@something.com> wrote in message
news:dbeu1l$p4$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
> 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?[/color]

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



Jerry Stuckle
Guest
 
Posts: n/a
#8: Jul 18 '05

re: Relative Paths and include files


mark wrote:[color=blue]
> 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:
>[color=green]
>> mark wrote:
>>[color=darkred]
>>> 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.
>>>
>>>[/color]
>>
>> 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.
>>
>>[/color][/color]

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.
jstucklex@attglobal.net
==================
Closed Thread