Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 05:03 PM   PM User | #1
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 280
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
directory problem with php include

Hi there.

I may be missing a trick here, but when using an include for my header.php I'm getting some problems with directory folders.

The problem happens when my current page isn't on the same level as the one I'm linking to, for example my 'gallery.php' is in a sub directory called 'gallery', but when I try to link to 'home.php' from this page, it looks for 'gallery/home.php'.

I know I can get around this by linking to "../home.php" but as i'm using an include for my header.php then this doesn't work, as I need to link to "home.php" on other pages higher in the directory tree.

Is there a novel way around this?

header.php
Code:
<div id="header-links" class="header-links">
	<ul class="inline">
		<li><a href="landing.php">new search</a></li>
		<li><a href="gallery/gallery.php">gallery</a></li>
		<li><a href="venues.php">venues</a></li>
	</ul>
</div>
paddyfields is offline   Reply With Quote
Old 02-12-2013, 05:48 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, this is per normal.
PHP's cwd comes from the executing script, not from the script requesting an inclusion. As you start stacking inclusions, than the filepaths no longer match up.
The answer is to always use relative from THIS script (the one calling the include). I don't recommend absolute paths as if you distribute the software that creates a little bit of a mess unless you want to precalculate everything or ask for explicit configurations, and I don't recommend pulling from $_SERVER since that then locks you into a web environment. So to include a path from above *this* script would be a simple:
PHP Code:
require_once __DIR__ '/../home.php'
Now if this script is included into /index.php for example and called with /subdir/thatscript.php, than /subdir/thatscript.php will resolve /subdir/../home.php as the path to include.

Simply prefixing with __DIR__ and working relative from this file's path eliminates the concern with inclusion depths and where the inclusion is called.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-12-2013, 07:00 PM   PM User | #3
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 280
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
Thanks a lot for your help Fou-Lu, but after playing around with your answer I'm still quite confused.

Say my include is this;

header.php
Code:
<ul class="inline">
	<li><a href="landing.php">new search</a></li>
	<li><a href="gallery/gallery.php">gallery</a></li>
</ul>
And my page I want to include it on is this;

gallery/gallery.php
Code:
<div id="container">
 	<?php require_once __DIR__ . '/../header.php'; ?>
 	<p>more page content</p>
</div>
I'm still getting a file path of 'gallery/gallery/gallery.php' if I'm on that page. This will however work perfectly when included on '(no subdir)/home.php'.

I realise that's because I'm not doing what you told me properly , but could you point me in the right direction?
paddyfields is offline   Reply With Quote
Old 02-12-2013, 07:03 PM   PM User | #4
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 280
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
I do appreciate you going to the effort of explaining in detail btw, I read through what you said about 10 times but it's just a bit over my head!
paddyfields is offline   Reply With Quote
Old 02-12-2013, 07:56 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ohhh wait I think you are looking at this: <li><a href="gallery/gallery.php">gallery</a></li>
That isn't the same as a PHP include. PHP has no control of this; it is a client specific operation. On the plus side, HTML is jailed to the public_html (or whatever it is configured as), so using /gallery/gallery.php would lock it into the site.com/gallery/gallery.php.
Using the base tag can also alter this. HTML links are always performed relative to the output as well, but it's not as much of a headache since it always considers the root as the site root and not filesystem root. Plus you cannot stack includes in the HTML world the same way you do in PHP.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
paddyfields (02-12-2013)
Old 02-12-2013, 08:08 PM   PM User | #6
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 280
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
I just added a '/' to the beginning of the link address and it's linking it from the localhost (exactly what i needed!)

So simple...

Thanks for your help.
paddyfields is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:53 PM.


Advertisement
Log in to turn off these ads.