-1

I wish to use header (includes css, js and images) and footer files located in a folder called /template. These files then get included in every file down the directory for example:

/template/header.php + footer.php
/home/index.php
/products/products.php
/products/hardware/types/hardware.php
/css
/js
/images

When i use:

include (__DIR__ . "/../template/header.php");

The file is included (although i need to change each file and ad more "/../" the further down the directory i go) but all the css, js and images are broken.

here is my header (located in header.php, one of the included files):

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="icon" type="image/gif" href="images/favicon.ico">
<link rel="stylesheet" type="text/css" href="../css/layout.css"/>
<link rel="stylesheet" type="text/css" href="../css/slider.css"/>
<link rel='stylesheet' type='text/css' href='../css/menus.css'/>
<link rel='stylesheet' type='text/css' href='../css/forms.css'/>
<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="all"/>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/jquery.elastic.source.js"></script>
<script type="text/javascript" src="../js/control.js"></script>

<title>APEC Energy - <?php echo($PageTitle);?> </title>
</head>

I am now trying to use a config file (which is hard to get working and still doesnt for me).

config.php (placed in root folder)(Do i need to place more information in the array, file path or directory path? if so how?):

<?php

$paths = array(
    "root" => "./",
        "controller"    => "controller",              
        "stylesheets"   => "css",
        "images"        => array(...),
        "javascript"    => array(...),

);

$projectName = "/";
function fInc($filePath){
    global $projectName;
    return '//'.$_SERVER['HTTP_HOST'].'/'.$projectName.'/'.$filePath;
}

?>

and in each .php file calling for the including file im using:

<?php
$rootDir = $_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1]);
require_once($paths['helper']['html']."template/header.php");
?>

When i use var_dump($paths), it prints out "NULL"

but just get a blank screen now?

thank you

alwayslearning
  • 411
  • 5
  • 18

2 Answers2

0

Try:

<?php
$basePath = dirname(__FILE__);
// Or __DIR__, since PHP 5.3.0

require_once($basePath . "relative/path/from/basePath"); 
// replace the string with your real relative path


Edits:

Q1) Yes. dirname(__FILE__) describes the absolute path of the directory your files are located in.

Q2) Not necessary. If you know the file which you want to include is just one level or two level up you can also use
For one level - include("../filename.php");
For two level - include("../../filename.php");
and so on..
Note: But I still recommend to use absolute paths.


For your reference:
PHP: Absolute vs. relative paths

Community
  • 1
  • 1
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
0

I always included a config.php in the first line of each php:

require_once($_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1])."/config.php");

This code should always find the root directory, and then include the config.php file. Note that you can put this file anywhere you want - it will always find it's location on its own. If you move the file to another directory subsequently, you don't have to change anything.

This config file then defines the paths to all subdirectories and other important files, relative to the root directory:

$paths = array(
    "root" => "",
        "controller"    => "controller",              
        "stylesheets"   => "css",
        "images"        => array(
             "icons"    => "img/icons",
             "controls" => "img/controls"
             ),
        "javascript"    => array(...),
        ...
);

This array means, that the root folder (your public_html) contains the directories "controller", "stylesheets", images" and so on. Images are either placed within "img/icons" or "img/controls".

All other files are included so:

require_once($paths['helper']['html']."/form.php");

This can be very useful, because it allows you to restructure your complete directory layout, and you only need to update your config.php.

And, last but not least, the config also contains a function like this:

$projectName = "YourProjectFolderName";
function fInc($filePath){
    global $projectName;
    return '//'.$_SERVER['HTTP_HOST'].'/'.$projectName.'/'.$filePath;
}

It can be used to convert paths which will be inserted into the HTML document.

Edit: Note, that defining an array with all paths is just a suggestion. It helped me in my projects, as I often restructure my project layout. If you don't do that in your project, you can just define a

$rootDir = $_SERVER['DOCUMENT_ROOT']."/".(explode ('/', $_SERVER['PHP_SELF'])[1]);

and then include files with

require_once($rootDir . "/home/index.php");

This way, you won't need the config.php anymore.

maja
  • 17,250
  • 17
  • 82
  • 125
  • ok looks great just need to understand some elements. 1. in the top satement in the config file, do i need to list the css or image (etc) files? or use code as is? 2. in the require_once function (i take it you are suggesting to use this function instead of include) do i just edit the"./form" to the file i want to be included and aev "helper" and "html" as is. 3. And project folder name is the top root folder name "public_html"? – alwayslearning Dec 21 '14 at 19:58
  • 1. It depends on the size of your project. If you have a big project, it might be useful to define an array with all existing directories and their paths, and then use it for includes. 2. Yes, I recomment require_once(); 3. I'm not sure about that. Either it's `public_html`, or `.` (if it is, your can remove it) – maja Dec 21 '14 at 20:00
  • ok great thank you so much! will give both a try and let you know. – alwayslearning Dec 21 '14 at 20:39
  • sorry can you expand your answer to show how you would enter files or dirs into the "images" => array? etc. is the first part "controller" or "css" the folder names in the root? – alwayslearning Dec 22 '14 at 10:30
  • also cant get the rootdir option to work? i placed both statements into changed the file location that was it but end up with a blank screen. would be nice to use this till i figure out how to use the config file option. good thing with this statment its universal and doesnt require me to keep adding /../../ the further i go down. thank you – alwayslearning Dec 22 '14 at 10:58
  • @david I extended the array-part. To your other problem: print the content of `$_SERVER['DOCUMENT_ROOT']` and `$_SERVER['PHP_SELF']`. Maybe your setup requires another path to find the root. – maja Dec 22 '14 at 14:13
  • i have updated he question to give more detail. maybe you can reference it to what im doing. getting confused now. – alwayslearning Dec 22 '14 at 15:15