July 22, 2012

php include (cannot get correct path)

Question by Sandro Dzneladze

I have a file that resides in:


I have another file in sub directory:


And in _gallerypage.php I have php include:

<?php include('../sidebar-left-big.php'); //left sidebar, category navigation and ads ?>

Error I get:

Warning: include(../sidebar-left-big.php) [function.include]: failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

Warning: include() [function.include]: Failed opening '../sidebar-left-big.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

It seems to me I’m doing everything correctly.

I thought that maybe problem is that _gallerypage.php is loaded via include in another file, so ../ relative to that leads to error. But error doesn’t say anything as to where it thinks path to sidebar-left-big.php is.

Answer by Jerzy Zawadzki

use include dirname(__FILE__).'/../sidebar-left-big.php';

Answer by Starx

Yes, You are right.

When you include the _gallerypage.php from another file, it does take the path relative to itself. So, you should fix this.

The best way, might be avoid such difficulties. There are number of ways to do this. Like, One would be define a global root in a constant and include every thing, everywhere as per it.

For example:

define("BASE" , "/wordpress"); //Define a base directory

//now whenever you are including and requirng files, include them on the basis of BASE


Please fill the form - I will response as fast as I can!