0

in my root folder of my domain i have files header.php and footer.php, i have created a new folder and file in the new folder and wanted to include the header and footer files from main. So the paths are: main folder: example.com/header.php new file: example.com/folder/new_file.php <- in this file i want to include the header.

I have tried:

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

and none of them work. Any help would be appreciated thanks.

3
  • The last one should work, assuming the file system structure matches the URL structure you're showing. Does it? How are you confirming that this works or not? Is there an error or warning in the PHP logs? Commented Feb 8, 2014 at 14:00
  • Hi David. I managed to get the last one to work ( not sure why it dint before) but now im facing the problem that even do all the text is there, it is not reading the css files inside the header.php file Commented Feb 8, 2014 at 14:08
  • in header i have the links to .css files + navbar, navbar shows all text but no css has been applied Commented Feb 8, 2014 at 14:09

3 Answers 3

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

it's will work .try may be its help in any of your files

Sign up to request clarification or add additional context in comments.

2 Comments

This also worked but it is not reading the links for css files with in the header file. All files on main root are displayed ok
try to use absolute paths from the webroot: href="/css/style.css"
0
include_once dirname(__DIR__).'/header.php';

This should work.

Comments

0

Thanks for all of your support. To answer my question thanks to all here are the answers.

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

and php include "../header.php";

DO work and i was able to include a file from main directory so thanks to david and code360.

second part of why the css file was not being read is because i had it like this in header.php file:

<link rel="stylesheet" href="css/bootstrap.css">

and missed the first "/"

Thanks again to code360

1 Comment

you welcome my friend Richard Epton :) we are here to help each other .give vote my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.