0

I have a file that resides in:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/sidebar-left-big.php

I have another file in sub directory:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php

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.

Tomer
  • 17,787
  • 15
  • 78
  • 137

3 Answers3

1

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

Jerzy Zawadzki
  • 1,975
  • 13
  • 15
  • Is there a way to learn what is correct path? can I print current file path somewhere so I can fix it without using dirname? –  Jul 22 '12 at 10:14
0

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

require(BASE."/admin/.../some.php");
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Yes, but is there a way to understand where file is called from? Something like pwd in bash? –  Jul 22 '12 at 10:12
0

Use the following snippet and save it as a file (e.g. config.php) in the root of your PHP application. Then you can go ahead and reference this file in all of your other PHP files to make use of the variables BASE_PATH and TOP_LEVEL_DIR

<?php

/**
 * @def (string) DS - Directory separator.
 */
define("DS","/",true);

/**
 * @def (resource) BASE_PATH - get a base path.
 */
define('BASE_PATH',realpath(dirname(__FILE__)).DS,true);
define('TOP_LEVEL_DIR', public_base_directory());

?>

<?php
function public_base_directory()
{
    //get public directory structure eg "/top/second/third"
    $public_directory = dirname($_SERVER['PHP_SELF']);
    //place each directory into array
    $directory_array = explode('/', $public_directory);
    //get highest or top level in array of directory strings
    $public_base = max($directory_array);

    return $public_base;
}
?>