3

I have a bunch of directories, and I have trouble including some files. Here's my structure.

index.php

vian/vian.php

includes/overall/head_content.php

includes/overall/head.php

Here is my head_content.php.

testing text
<?php include 'includes/overall/head.php'; ?>
<div class="wrapper row3">
    <div id="container" class="clear"> 
        <div id="content">

I want to include that file in vian/vian.php

I use '../includes/overall/head_content.php';, and the path works since 'testing text' shows up. But it cannot include includes/overall/head.php. I vaguely understand why it doesn't work, but I can't figure out how to fix it.

'../includes/overall/head_content.php'; is used on all my pages, including index.php, which is in my root directory.

hakre
  • 193,403
  • 52
  • 435
  • 836
Sarah
  • 47
  • 6
  • 2
    Checkout the [`__DIR__` magic constant](http://php.net/manual/en/language.constants.predefined.php). You can then include relative to the current's file directory: `__DIR__ . '/../vian/vian.php'` and similar. – hakre Oct 26 '12 at 14:11

2 Answers2

3

In your structure:

 + index.php
 + vian
 |  ` vian.php
 ` includes
    + overall
        + head_content.php
        ` head.php

Each file has a specific relationship to the other. Because of that you can make use of relative links from one file to another.

Of help in PHP for that is the __DIR__ magic constant. It contains the directory in each file automatically. You can use it, to make the relative path an absolute one. Absolute paths are very robust.

Examples:

  • From index.php to includes/overall/head.php:
    __DIR__ . '/includes/overall/head.php'
  • From includes/overall/head.php to vian/vian.php:
    __DIR__ . '/../../vian/vian.php'
  • From includes/overall/head_content.php to includes/overall/head.php:
    __DIR__ . '/head.php'

And so on and so forth.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Actually, I want to from from vian.php to head.php. – Sarah Oct 26 '12 at 14:24
  • That would be the second example. You go two steps up and then again down into the *vian* directory. – hakre Oct 26 '12 at 14:25
  • Then there is something I'm not getting... I do not want to include vian.php anywhere. I want to include head.php in head_content.php, so it can show up in vian.php – Sarah Oct 26 '12 at 14:27
  • @Sarah: It all works the same. I added a third example showing that case. Maybe it helps to clarify this for you. Those two files are in the same directory, so it's even pretty straight forward. – hakre Oct 26 '12 at 14:30
  • Ah, works. I forgot the `/` before the `../`. Now, should I use this on my whole website from now on? Or is it unnecessary? – Sarah Oct 26 '12 at 14:41
  • 1
    Yes, you should use that at each position. So you can train this a little and also you have it the same everywhere. Then you don't need to think so much. Also if you enable error reporting for development PHP will show you the path if something goes wrong. – hakre Oct 26 '12 at 14:47
0

Try

<?php include 'head.php'; ?>
Mohit Mehta
  • 1,283
  • 12
  • 21