Problem defining paths inside included PHP file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grahamporter
Forum Newbie
Posts: 3
Joined: Mon Feb 04, 2008 2:52 am

Problem defining paths inside included PHP file

Post by grahamporter »

I'm setting up my first website using DW8/PHP5 and apache for local testing.

I've created a PHP file with code for a navigation bar which has html code pointing at various images. When I create pages in the site root directory this works fine, as the html image links are set to "images/some.gif".

However, I have set up a tree of directories for my content and when I use the include in pages below the root, the image file are no longer found as the image links start looking in the current directory.

I assume I need to add some code to the include file so the img src tag is always going to look in the correct directory no matter where the page is created. I've been trying for ages to figure this out, but so far without success. I am on the wrong track here? Here's an example:

Navbar.php
<div id="logoheader"><img src="Images/some.gif"/></div>

SiteRoot contains:
page_one
Navbar.php

SiteRoot\Images contains:
some.gif

SiteRoot\Content contains:
page_two

page_one works fine as it is created in the site root. But page_two doesn't.

Any help would be appreciated by a frustrated newbie! :-)

Graham
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem defining paths inside included PHP file

Post by Christopher »

Since page_two.php is in the Content directory, the relative path to the images would be "../images/some.gif"". The absolute path to the images would be something like "http://localhost/SiteRoot/images/some.gif"".
(#10850)
grahamporter
Forum Newbie
Posts: 3
Joined: Mon Feb 04, 2008 2:52 am

Re: Problem defining paths inside included PHP file

Post by grahamporter »

Thanks for the reply.

Can't use the "../" syntax in the include file as it's included into pages in two different directories, but the absolute path seems to have done the trick.

One more silly question. When I publish the site I assume I'll have to change the references to localhost to the actual address eg. http://www.somesite.com, or is the localhost always defined as the correct address?

Thanks again for the prompt reply.

Graham
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem defining paths inside included PHP file

Post by Christopher »

You will need to change it to the correct URL.
(#10850)
grahamporter
Forum Newbie
Posts: 3
Joined: Mon Feb 04, 2008 2:52 am

Re: Problem defining paths inside included PHP file

Post by grahamporter »

Thought so.

Thanks for the assist.
Post Reply