includes are really messing me up

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
   I have these files


The page "" contains the line

<?php include("../"); ?>

The page home_page.php contains

<?php include("util_fns.php"); ?>

and admin_home.php contains

<?php include("../util_fns.php"); ?>

but when I visit admin_home, I get the error
"main(../include/ failed to open stream: No such file or
directory".  If I change the line in "" to:

<?php include("../../"); ?>

the admin_home.php works fine, but the home_page.php page breaks.  How
can I write the include line in "" to accommodate both

Thanks, - Dave

Re: includes are really messing me up

D. Alvarado wrote:
Quoted text here. Click to load it
What is messing things up is that the directory the file is in is where
the include is oriented from.

To make the orientation the same use $_SERVER['DOCUMENT_ROOT']
as the base part in all the files and the includes too.

include($_SERVER['DOCUMENT_ROOT'] . "../db_info.php");
if the include_path has /include in it.


include($_SERVER['DOCUMENT_ROOT'] . "../include/db_info.php");
if the include_path does not..

Re: includes are really messing me up

Quoted text here. Click to load it

Make your include_path setting

include_path /path/to/include/

where /path/to/ is the path to the first of the items you listed.

Then just use

<?php include (''); ?>
or better still, use require_once () instead of include ()


Re: includes are really messing me up

Quoted text here. Click to load it

The best solution I have found so far is the following code:

function include_once_relative($path) {
  $from = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
  $to = realpath(dirname(__FILE__)."/".$path);


function relative_path ($targetfile, $basedir = '.') {
   $basedir    = realpath ($basedir);
   $targetfile = realpath ($targetfile);

   // on windows, check that both paths are on the same drive
   if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
       return false;

   // split each path into its directories
   $base_parts  = split ('\/', str_replace ('\', '/', $basedir));
   $target_parts = split ('\/', str_replace ('\', '/', $targetfile));

   // ensure that there are no empty elements at the end (c:\ would cause it)
   for ($i = count($base_parts) - 1; $i >= 0; $i--) {
       if ($base_parts[$i] == '') {
           unset ($base_parts[$i]);
       } else {
   for ($i = count($target_parts) - 1; $i >= 0; $i--) {
       if ($target_parts[$i] == '') {
           unset ($target_parts[$i]);
       } else {

   // get rid of the common directories at the beginning of the paths
   $common_count = 0;
   for ($i = 0; $i < count($base_parts); $i++) {
       if ($target_parts[$i] == $base_parts[$i]) {
       } else {
   for ($i = 0; $i < $common_count; $i++) {
       unset ($base_parts[$i]);
       unset ($target_parts[$i]);

   // build the resulting string
   $cnt = count($base_parts) - 1;
   if ($cnt < 1) {
    $cnt = 0;

   return str_repeat ('../', $cnt).implode('/', $target_parts);

Stick it into a library file which you /can/ include - and then
you can use:

"include_once_relative" in place of "include_once" - and it takes
account of a range of situations - including dealing with the
problem of the case where the path being specified is relative
to another included file - rather than the page being displayed.

Any improvements welcomed.
 |im |yler /  Remove lock to reply.

Site Timeline