peardrops
peardrops

Reputation: 109

Relative file paths for links within a PHP include

I have a header.php file that I'm using as an include on every page of my website. Within it are the site's navigation links.

Note: I don't want to use absolute file paths as I have no idea of the folder structure of the server on which the site is going to be uploaded. It needs to work no matter where the website is placed on the server. With that in mind I have two questions:

1) Is there some PHP I can apply to the beginning of the file path whenever I include the header.php? My website will have many sub-directories and it will simply be too time-consuming mentally traversing the site to point to the folder that it resides in each time I want to include it.

2) How can I ensure that the navigation links within header.php are always correct? e.g. a link back to the home page would be '../index.php' if it's one directory below, but anywhere else it wouldn't work.

I would greatly appreciate help, thanks.

Upvotes: 0

Views: 3419

Answers (3)

Henrik Carlström
Henrik Carlström

Reputation: 153

First make a php file that will contain your paths (name it for example ini.php and put it in folder ini):

defined("YOUR_BASE_PATH")
    or define("YOUR_BASE_PATH", realpath(dirname(__FILE__) . '/..'));

Then in your header.php do the following (include ini file that contains your path defines):

include_once(dirname(__FILE__) . "/ini/ini.php");
include_once(YOUR_BASE_PATH  . "/nocache.php");

And now you easily use paths within your project without hassle! Fairly good solution and you can move the folder as you like, as long as you update the ini file!

Now that you have set it up like this, you can change all your links to use the actual path:

<a href="<?php echo PathToUrl(YOUR_BASE_PATH . "/index.php"); ?>">test link</a>

Make a simple function in your header.php for PathToUrl:

function PathToUrl($path)
{
    $path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path)
    return $path;
}

That should do it.

Upvotes: 1

Richard87
Richard87

Reputation: 1621

use the magic constant __DIR__...

From PHP's documentation:

DIR The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory.

then use it like this:

$file = DIR . "/../somefile.txt";

also, in header.php (or any other script that is always loaded first, you can set a constant:

const PROJECT_DIR = __DIR__ . "/..";

and then always refer to PROJECT_DIR

2

This one is tricky :/ I don't think it's possible, and it also sounds very unsecure (if you can access all files on the server by just adding a bunch of ..s, for example: ../../../../../../../etc/passwd

Good luck!

Upvotes: 0

Rushikumar
Rushikumar

Reputation: 1812

Yes, by using PHP magic constants. Try the following:

__DIR__

Add that to your include links prior to the start of the string... Example:

require __DIR__ . "/file_name.php";

You can read more about it here: http://www.php.net/manual/en/language.constants.predefined.php

Upvotes: 0

Related Questions