Connecting Tech Pros Worldwide Help | Site Map

Relative Paths and include files

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 12:15 AM
mark
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 18th, 2005, 12:55 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default 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
==================
  #3  
Old July 18th, 2005, 12:55 AM
mark
Guest
 
Posts: n/a
Default 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]
  #4  
Old July 18th, 2005, 02:25 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default 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
==================
  #5  
Old July 18th, 2005, 02:25 AM
Geoff Muldoon
Guest
 
Posts: n/a
Default 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
  #6  
Old July 18th, 2005, 07:35 AM
mark
Guest
 
Posts: n/a
Default 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]
  #7  
Old July 18th, 2005, 09:45 AM
Tony Marston
Guest
 
Posts: n/a
Default 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



  #8  
Old July 18th, 2005, 11:15 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default 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
==================
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.