[SOLVED] include link problem when in different folders

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
fxchain
Forum Newbie
Posts: 9
Joined: Tue Dec 09, 2003 12:05 pm

include link problem when in different folders

Post by fxchain »

I searched for an answer, but no luck.

I have header.php in root folder:

Code: Select all

<html><body>
<a href="folder1/folder2/file1.php">File1</a><br>
<a href="folder1/folder2/file2.php">File2</a>
</body></html>
I have index.php in root:

Code: Select all

<html><body>
<?php include('header.php');?>
</body></html>
(php parses links ok)

I then have file1 in /folder1/folder2/:

Code: Select all

<html><body>
<?php include('../../header.php');?>
</body></html>
But when php parse file1.php, the links from header.php are wrong, they go to:
folder1/folder2/folder1/folder2/file1.php
as oppose to just:
folder1/folder2/file1.php

It duplicates locations. The header links + it's current location.

Please help.
Francois
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You could use /folder/path/file

And if you tweak the php include_path you don't have to bother about the relative path to header.php either.
fxchain
Forum Newbie
Posts: 9
Joined: Tue Dec 09, 2003 12:05 pm

Post by fxchain »

You could use /folder/path/file
Not quite sure what you mean...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

absolute paths, versus relative paths.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

#include
#require...
require_once($_SERVER['DOCUMENT_ROOT']."/include/path/page.php");
fxchain
Forum Newbie
Posts: 9
Joined: Tue Dec 09, 2003 12:05 pm

Post by fxchain »

Found much easier answer:

Changed header.php in root folder:

Code: Select all

<html><body> 
<a href="/folder1/folder2/file1.php">File1</a><br> 
<a href="/folder1/folder2/file2.php">File2</a> 
</body></html>
Note the / in front of path. That made it work.

Thanx for all your help
chemburn
Forum Newbie
Posts: 1
Joined: Mon Aug 30, 2004 5:54 pm

Post by chemburn »

This question isn't really realated to PHP, but it sorta is.

He's talking about when the page is rendered in the browser, the links are linked to folder1/folder2/folder1/folder2/file1.php. The way to fix this is prepend all of your <a tags, with a /. So in your instance.

Code: Select all

<html><body> 
<a href="/folder1/folder2/file1.php">File1</a><br> 
<a href="/folder1/folder2/file2.php">File2</a> 
</body></html>
OR, add a base href tag to the document. So that no matter where in the site the header file is included, the links point to the right place on on the root of the site.

Code: Select all

<html><body> 
<base href="/">
<a href="folder1/folder2/file1.php">File1</a><br> 
<a href="folder1/folder2/file2.php">File2</a> 
</body></html>
I'd recommend the first option, it's more work, but it also wont effect other links on the other pages that may be included. Just get in the habbit of prepending a / to all of your links.

Have fun when you move it to another folder. :)

"Moderator, I wonder what happened to my first amendment rights???"
Post Reply