Php includes a file from another directory

Here is an example structure:

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

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

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

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

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

Now everything works fine as it is. Then I will embed the following code in the article1.php file:

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

Content (menu and other html) is displayed correctly, however CSS will not be displayed / recognized at all. Basically, what happens is that the header file is included, but the server does not respect parental accounting. For CSS to display correctly, I will need to change the link rel for CSS to ../css/style.css , but if I do this, it will not work with files located in the main directory.

I hope I have clarified my problem. What am I doing wrong? How to include files from different directories and save links inside them?

+6
source share
8 answers

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

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

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

Note. This affects not only the style sheets, but also all relative links in the document.

+20
source

This is due to how pathing in includes works. I recommend, when possible, keep track of things from the doc root.

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

This will work in any of your files.

+13
source

Instead of using relative paths:

 href="css/style.css" 

use absolute paths from the website:

 href="/css/style.css" 
+5
source

You must add your css file from the root. So, /css/style.css, so it will always start from the root, and then go down from there. I believe that this should solve your problem in all cases.

+3
source

First of all, the problem is that your link to your CSS files is incorrect. It’s best to look at the HTML output (view source) from the page. So let's break it (form index.php):

Index.php is located in domain.tld/index.php . Your css files are located in domain.tld/css/* . When viewing an index file your title

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

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

 domain.tld/include/css/style.css 

This means that you need to create a full link or change your relative. The easy way, since CSS is at the root, it's easy to use

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

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

+3
source

Can I explain your problem and how to solve it.

An include in php works like in C. When you include a page that copies / pastes the contents of the page in the first.

So there are two files:

include.php

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

main.php

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

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

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

So, if you need to include an element on your page. You have two options:

1. absolute path (best way)

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

2. variable

You can use a variable containing the relative path to the root directory of your repository / website. Therefore, every time you must include a page or create it, you must define the variable as follows:

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.

+2
source

You have two options.

  • Pass the relative path to the included file

t

 <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" /> 
+1
source

Using relative inclusions from the filed already called as inclusions is always a problem, every time you specify a relative location, replace

 ../some_directory/some_file.php 

with

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

dirname(__FILE__) - current directory.

+1
source

All Articles