includes are really messing me up

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Alvarado

    #1

    includes are really messing me up

    Hi,
    I have these files

    /include/db_info.inc
    /html_root/util_fns.inc
    /html_root/home_page.php
    /html_root/admin/admin_home.php

    The page "util_fns.i nc" contains the line

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

    The page home_page.php contains

    <?php include("util_f ns.php"); ?>

    and admin_home.php contains

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

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

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

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

    Thanks, - Dave
  • David Zawislak

    #2
    Re: includes are really messing me up

    D. Alvarado wrote:[color=blue]
    > Hi,
    > I have these files
    >
    > /include/db_info.inc
    > /html_root/util_fns.inc
    > /html_root/home_page.php
    > /html_root/admin/admin_home.php
    >
    > The page "util_fns.i nc" contains the line
    >
    > <?php include("../db_info.inc"); ?>
    >
    > The page home_page.php contains
    >
    > <?php include("util_f ns.php"); ?>
    >
    > and admin_home.php contains
    >
    > <?php include("../util_fns.php"); ?>
    >
    > but when I visit admin_home, I get the error
    > "main(../include/db_info.inc): failed to open stream: No such file or
    > directory". If I change the line in "util_fns.i nc" to:
    >
    > <?php include("../../db_info.inc"); ?>
    >
    > the admin_home.php works fine, but the home_page.php page breaks. How
    > can I write the include line in "util_fns.i nc" to accommodate both
    > situations?
    >
    > Thanks, - Dave[/color]
    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.

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

    or

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







    Comment

    • Martin Lucas-Smith

      #3
      Re: includes are really messing me up



      [color=blue]
      > /include/db_info.inc
      > /html_root/util_fns.inc
      > /html_root/home_page.php
      > /html_root/admin/admin_home.php
      >
      > The page "util_fns.i nc" contains the line
      >
      > <?php include("../db_info.inc"); ?>
      >
      > The page home_page.php contains
      >
      > <?php include("util_f ns.php"); ?>
      >
      > and admin_home.php contains
      >
      > <?php include("../util_fns.php"); ?>
      >
      > but when I visit admin_home, I get the error
      > "main(../include/db_info.inc): failed to open stream: No such file or
      > directory". If I change the line in "util_fns.i nc" to:
      >
      > <?php include("../../db_info.inc"); ?>
      >
      > the admin_home.php works fine, but the home_page.php page breaks. How
      > can I write the include line in "util_fns.i nc" to accommodate both
      > situations?[/color]

      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 ('db_info.inc') ; ?>
      or better still, use require_once () instead of include ()


      Martin

      Comment

      • Tim Tyler

        #4
        Re: includes are really messing me up

        D. Alvarado <laredotornado@ zipmail.com> wrote or quoted:
        [color=blue]
        > I have these files
        >
        > /include/db_info.inc
        > /html_root/util_fns.inc
        > /html_root/home_page.php
        > /html_root/admin/admin_home.php
        >
        > The page "util_fns.i nc" contains the line
        >
        > <?php include("../db_info.inc"); ?>
        >
        > The page home_page.php contains
        >
        > <?php include("util_f ns.php"); ?>
        >
        > and admin_home.php contains
        >
        > <?php include("../util_fns.php"); ?>
        >
        > but when I visit admin_home, I get the error
        > "main(../include/db_info.inc): failed to open stream: No such file or
        > directory". If I change the line in "util_fns.i nc" to:
        >
        > <?php include("../../db_info.inc"); ?>
        >
        > the admin_home.php works fine, but the home_page.php page breaks. How
        > can I write the include line in "util_fns.i nc" to accommodate both
        > situations?[/color]

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

        function include_once_re lative($path) {
        $from = realpath(dirnam e($_SERVER["SCRIPT_FILENAM E"]));
        $to = realpath(dirnam e(__FILE__)."/".$path);

        include_once(re lative_path($to ,$from));
        }

        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_par ts) - 1; $i >= 0; $i--) {
        if ($base_parts[$i] == '') {
        unset ($base_parts[$i]);
        } else {
        break;
        }
        }
        for ($i = count($target_p arts) - 1; $i >= 0; $i--) {
        if ($target_parts[$i] == '') {
        unset ($target_parts[$i]);
        } else {
        break;
        }
        }

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

        // build the resulting string
        $cnt = count($base_par ts) - 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_r elative" in place of "include_on ce" - 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 http://timtyler.org/ tim@tt1lock.org Remove lock to reply.

        Comment

        Working...