weird include() problem - HELP my library is #?@*ed!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Long

    weird include() problem - HELP my library is #?@*ed!!

    Hey,

    I've written a custom HTML library using many PHP scripts as seperate
    files (just like I'd do a java project) and I'm having some problems
    where I'm including scripts in different directories from already
    included scripts... basically where there'a an include chain spanning
    multiple directories.

    I've reduced the problem to its core:

    Imagine you have a website consisting of 4 PHP files:

    /top_site.php
    /inlcudes/top_lib.php
    /includes/lib/lib1/lib1.php
    /includes/lib/lib2/lib2.php

    the contents of which are:

    top_site.php = <? include_once("i ncludes/top_lib.php"); ?>
    top_lib.php = <? include_once("l ib/lib1/lib1.php"); ?>
    lib1.php = <? include_once(". ./lib2/lib2.php"); ?>
    lib2.php = <? echo "Hello World!"; ?>

    now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
    saying that the include within lib1.php can't find lib2.php...

    ....BUT if I go directly to
    http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you'll see that
    it includes lib2.php fine!!!

    Try it for yourself; it won't take a minute to set up.

    I've checked my include path and it looks fine (having '.' in it), so
    I'm basically stuck there. Oh, and I'm running PHP version 4.2.2 on
    Linux RH9/Apache 2.

    Has anyone else seen this before? Have any ideas? Cos I don't really
    want to have to convert everything over to absolute (not sure if
    this'll work either) and I've written LOTS of code! D'OH!

    Thanks,
    Rob Long.
  • ChronoFish

    #2
    Re: weird include() problem - HELP my library is #?@*ed!!


    "Rob Long" <bobalong@gmx.n et> wrote in message news:95581389.0 401071130.e280a 1c@posting.goog le.com...[color=blue]
    > Hey,
    >
    > I've written a custom HTML library using many PHP scripts as seperate
    > files (just like I'd do a java project) and I'm having some problems
    > where I'm including scripts in different directories from already
    > included scripts... basically where there'a an include chain spanning
    > multiple directories.
    >
    > I've reduced the problem to its core:
    >
    > Imagine you have a website consisting of 4 PHP files:
    >
    > /top_site.php
    > /inlcudes/top_lib.php
    > /includes/lib/lib1/lib1.php
    > /includes/lib/lib2/lib2.php
    >
    > the contents of which are:
    >
    > top_site.php = <? include_once("i ncludes/top_lib.php"); ?>
    > top_lib.php = <? include_once("l ib/lib1/lib1.php"); ?>
    > lib1.php = <? include_once(". ./lib2/lib2.php"); ?>
    > lib2.php = <? echo "Hello World!"; ?>
    >
    > now when I go to http://YOUR_WEBSITE_ROOT/top_site.php I get an error
    > saying that the include within lib1.php can't find lib2.php...
    >
    > ...BUT if I go directly to
    > http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php then you'll see that
    > it includes lib2.php fine!!!
    >
    > Try it for yourself; it won't take a minute to set up.[/color]


    Hi Rob,

    The includes are relative to the web address - not relative to the code.

    So http://YOUR_WEBSITE_ROOT/top_site.php is looking for /webroot/../lib2/lib2.php - which doesn't exist

    and http://YOUR_WEBSITE_ROOT/includes/lib1/lib1.php is looking for
    /webroot/includes/lib1/../lib2/lib2.php - which does exist.


    What you want is something like:

    require_once ($_SERVER['DOCUMENT_ROOT']."/includes/lib2/lib2.php");


    The way I use the includes is like this:

    Imagine your directory as follows

    /index.php
    /main.php
    /inc/menu.php
    /content1/index.php
    /content1/main.php
    /content2/index.php
    /content2/main.php

    File /index.php:
    require_once ($_SERVER['DOCUMENT_ROOT']."/inc/menu.php");
    require_once ("main.php") ;

    File /inc/menu.php
    Option 1 Option 2 Option 3<br>

    Files /content1/index.php and /content2/index.php:
    require_once ($_SERVER['DOCUMENT_ROOT']."/index.php");

    /main.php
    Welcome to LocalHost!

    /content1/main.php
    Welcome to Content 1!

    /content2/main.php
    Welcome to Content 2!


    What does all of this give you?


    Option 1 Option 2 Option 3
    Welcome to LocalHost!


    Option 1 Option 2 Option 3
    Welcome to Content 1!


    Option 1 Option 2 Option 3
    Welcome to Content 2!


    Now updating Menu updates the menu for all pages on the site. Same could be done for headers, footers, etc.


    -CF


    Comment

    • Pedro Graca

      #3
      Re: weird include() problem - HELP my library is #?@*ed!!

      Rob Long wrote:[color=blue]
      > Cos I don't really want to have to convert everything over to absolute[/color]

      Try

      <?php
      set_include_pat h(get_include_p ath() .
      ':/:/includes:/includes/lib/lib1:/includes/lib/lib2');
      ?>

      to have php use all those directories as the source for the include.
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Rob Long

        #4
        Re: weird include() problem - HELP my library is #?@*ed!!

        "ChronoFish " <deja@chronofis h.com> wrote in message news:<kwZKb.248 50$ti2.13240@la keread03>...[color=blue]
        > "Rob Long" <bobalong@gmx.n et> wrote in message news:95581389.0 401071130.e280a 1c@posting.goog le.com...
        > What you want is something like:
        >
        > require_once ($_SERVER['DOCUMENT_ROOT']."/includes/lib2/lib2.php");[/color]

        Thanks a lot mate. Shame that the PHP parser isn't smart enough to
        have some kind of include counter, writing all the includes to the
        output script at the end, therefore allowing include paths to be
        relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
        more sense to me. You wouldn't have any problems taking a library as a
        package and moving it from project to project etc.

        Sigh.

        Rob.

        Comment

        • Pedro Graca

          #5
          Re: weird include() problem - HELP my library is #?@*ed!!

          Rob Long wrote:[color=blue]
          > Thanks a lot mate. Shame that the PHP parser isn't smart enough to
          > have some kind of include counter, writing all the includes to the
          > output script at the end, therefore allowing include paths to be
          > relative from the SCRIPT IN WHICH THEY ARE DECLARED! Which makes a lot
          > more sense to me. You wouldn't have any problems taking a library as a
          > package and moving it from project to project etc.[/color]

          Try this

          #v+
          <?php
          $rel1 = '../../include.php';
          $rel2 = 'forward/path/include.php';

          include preg_replace('@ ^(.*/)[^/]+$@', '$1', __FILE__) . $rel1;
          include preg_replace('@ ^(.*/)[^/]+$@', '$1', __FILE__) . $rel2;
          ?>
          #v-

          Happy Coding :)
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • Geoff Berrow

            #6
            Re: weird include() problem - HELP my library is #?@*ed!!

            I noticed that Message-ID: <kwZKb.24850$ti 2.13240@lakerea d03> from
            ChronoFish contained the following:
            [color=blue][color=green]
            >> Try it for yourself; it won't take a minute to set up.[/color]
            >
            >
            >Hi Rob,
            >
            >The includes are relative to the web address - not relative to the code.[/color]

            How come I have a file in a subdirectory which has

            include '1.php';

            which includes file in that subdirectory just fine.

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • ChronoFish

              #7
              Re: weird include() problem - HELP my library is #?@*ed!!


              "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message news:sv410099sv 1a3s4l917bnql9o 7dp8cen63@4ax.c om...[color=blue]
              > I noticed that Message-ID: <kwZKb.24850$ti 2.13240@lakerea d03> from
              > ChronoFish contained the following:[color=green]
              > >
              > >The includes are relative to the web address - not relative to the code.[/color]
              >
              > How come I have a file in a subdirectory which has
              >
              > include '1.php';
              >
              > which includes file in that subdirectory just fine.[/color]

              Don't know. If your search path includes the subdirectory, or if the subdirectory is the directory you're looking at it would work.
              Otherwise it shouldn't.

              -CF


              Comment

              Working...