Page 1 of 1

Include a file in a different directory

Posted: Mon May 09, 2011 4:14 pm
by nyo
Hi,

I know this is a very basic question but I am stuck.

I have two files:

includes/header.php
gallery/gallery.php

I am trying to include header.php into gallery.php but it gives error.

Here is the code I'm using in gallery.php:
<?php include '/includes/header.php'; ?>
I also tried:
<?php include './includes/header.php'; ?>

Re: Include a file in a different directory

Posted: Mon May 09, 2011 4:45 pm
by Tino
On gallery.php you would use:
include('../includes/header.php');
../ essentially means 'one directory level above'.

Re: Include a file in a different directory

Posted: Sun May 15, 2011 4:56 am
by nyo
I guess, I couldn't solve this yet.

I have the following file structure:

index.php
includes/header.php
includes/functions.php
english/eng.php


and their include codes as its simplest:

header.php
include functions.php

index.php
include header.php

eng.php
include header.php

When I use <?php include '../includes/header.php'; ?> in eng.php and <?php include '../includes/functions.php'; ?> in header.php, eng.php works fine but since index.php also has an include to header.php, it tries to look one upper directory and gives "No such file in the directory" error.

Am I following a wrong practice or is there a solution for this?

Re: Include a file in a different directory

Posted: Sun May 15, 2011 5:51 am
by Tino
Yes. You should simply try to include includes/header.php.

What I personally prefer to do, and what Jacek also does in his tutorials, is make a variable which holds the path to a certain location, and include everything with that as a base. I believe Jacek uses
$path = dirname(__FILE__);

Re: Include a file in a different directory

Posted: Sun May 15, 2011 12:10 pm
by nyo
I haven't gone through that template tutorial yet but I will do it now. Thanks Tino.