Include a file in a different directory

Ask about a PHP problem here.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Include a file in a different directory

Post 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'; ?>
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Include a file in a different directory

Post by Tino »

On gallery.php you would use:
include('../includes/header.php');
../ essentially means 'one directory level above'.
Please check out my CodeCanyon items.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Include a file in a different directory

Post 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?
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Include a file in a different directory

Post 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__);
Please check out my CodeCanyon items.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Include a file in a different directory

Post by nyo »

I haven't gone through that template tutorial yet but I will do it now. Thanks Tino.
Post Reply