coldpumpkin
coldpumpkin

Reputation: 714

php include file from another directory

Here is a structure example:

/main
 /css
  style.css
 /include
  article1.php
  article2.php
 header.php
 index.php

In my header.php I have the following code for the css:

<link rel="stylesheet" type="text/css" href="css/style.css" />

And, for example, in my index.php I have the following as the first line:

<?php include 'header.php'; ?>

Now, everything works fine as it is. Next I'm going to insert the following code in the article1.php file:

<?php include '../header.php'; ?>

The contents (menus and other html) are displayed correctly, however the CSS won't be displayed/recognized at all. Basically what's happening is that the header file is being included but the server isn't respecting the directory parenting. For the CSS to be displayed correctly I'd have to change the link rel for the CSS to ../css/style.css, but if I do so it won't work on files located in the main directory.

I hope I made my problem clear. What am I doing wrong? How can I include files from different directories and preserve the links inside them?

Upvotes: 8

Views: 60548

Answers (8)

mikevoermans
mikevoermans

Reputation: 4007

It has to do with how pathing works in includes. I recommend pathing things from the doc root whenever possible.

<?php include $_SERVER['DOCUMENT_ROOT'] . '/header.php' ;

That will work in any of your files.

Upvotes: 15

Xavier S.
Xavier S.

Reputation: 1157

May I explain your problem and how to solve it.

An include in php works as in C. When you include a page that copy/paste content of the page into the first one.

So, there are two files:

include.php

<?php
$var = 'PHP';
?>

main.php

<?php
include 'include.php';
echo $var; // affiche 'PHP'
?>

When you request for "main.php" page you will get the following:

<?php
$var = 'PHP';
echo $var; // affiche 'PHP'
?>

So if you need to include an element to your page. You have two choices:

1. absolute path (better way)

Instead of using relative path, use absolute path. This way allows you to include a file from everywhere without of use of the current path.

2. variable

You can use a variable containing the relative path to the root directory of your repository/website. So each time you have to include a page or create one you have to define a variable as following:

include.php

<?php
$var = 'PHP';
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$currentPath."css/style.css\" />";
?>

main.php

<?php
# path to the root of the website
$currentPath = "../";

include 'include.php';
echo $var; // affiche 'PHP'
?>

To learn more about include, see this page

Upvotes: 2

Matt
Matt

Reputation: 303

In your site's <head> section, insert a <base> element with the href attribute. Set the href attribute to the base URL of your website, and all relative requests will be sent through that base URL.

<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />

With a correctly-set base tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css.

Note: this not only affects stylesheets, but all relative links in the document.

Upvotes: 20

SteeveDroz
SteeveDroz

Reputation: 6124

Using relative includes from filed already called as includes is always a problem, each time you specify a relative location, replace your

../some_directory/some_file.php

with

dirname(dirname(__FILE__)) . '/some_directory/some_file.php'

dirname(__FILE__) is the current directory.

Upvotes: 3

Philipp
Philipp

Reputation: 15629

You have two options

  1. Pass the relative path to the included file

ie.

<link rel="stylesheet" type="text/css" href="<?=$path ?>css/style.css" />

and

$path = "../";
include "header.php";

or 2. you use absolute paths to your files

<link rel="stylesheet" type="text/css" href="/css/style.css" />

Upvotes: 1

UnholyRanger
UnholyRanger

Reputation: 1971

First off, the problem is that your link to your CSS files is wrong. The best thing to do is look at the output of the HTML (view source) from the page. So lets break it down (form index.php):

Index.php is located at domain.tld/index.php. Your css files are located at domain.tld/css/*. When viewing the index file, your header is

<link rel="stylesheet" type="text/css" href="css/style.css" />

which works because you are at the top of the directory (or root) and the links are made relative to this directory. Now when you go up to domain.tld/include/article1.php, you are no longer at the root. It is trying to access:

domain.tld/include/css/style.css

This means you have to build the full link or change your relative. The easy way since the CSS is at the root is just to use the following

<link rel="stylesheet" type="text/css" href="/css/style.css" />
                                             ^

This means it will look at the root of the domain for the css directory as such domain.tld/css/styles.css. If your top directory is /main, use /main/css/styles.css

Upvotes: 3

bubba
bubba

Reputation: 3847

Instead of using relative paths:

href="css/style.css"

use absolute paths from the webroot:

href="/css/style.css"

Upvotes: 5

Jacob Waller
Jacob Waller

Reputation: 4229

You should include your css file from the root. So /css/style.css so way it will always start at the root and then go down from there. I believe that should fix your problem in all cases.

Upvotes: 3

Related Questions