Using Relative Paths With include()

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

    Using Relative Paths With include()

    I am playing around with some PHP code. I want to put in an include()
    function so I can include existing HTML code and output it to the browser.

    Lo and behold PHP does not support relative paths with the include()
    function! (How shortsighted can you get?) Is there any way at all to use
    relative paths with include()? Any hacks? If I use an absolute filepath,
    everything is fine. But I don't want to do that--I can't do that. I want
    to use the filepath relative to the currently executing PHP file.

    Somewhere I read something about using an .htaccess file and putting
    something in it that will add an "include" for the HTML file I want to
    include without me having to change php.ini (I won't have access to php.ini
    on the production server anyway.). That way I could just drop it in my
    webroot directory and everything would be fine.

    Help will be appreciated.

    Thanks.

  • Rik

    #2
    Re: Using Relative Paths With include()

    MBS wrote:[color=blue]
    > I am playing around with some PHP code. I want to put in an include()
    > function so I can include existing HTML code and output it to the
    > browser.
    >
    > Lo and behold PHP does not support relative paths with the include()
    > function![/color]

    It does, works perfectly here in PHP4 and PHP5.
    Search your code for errors.

    Grtz,

    Rik


    Comment

    • MBS

      #3
      Re: Using Relative Paths With include()

      "Rik" <luiheidsgoeroe @hotmail.com> wrote in news:dlf7km$6fa $1
      @netlx020.civ.u twente.nl:
      [color=blue]
      > MBS wrote:[color=green]
      >> I am playing around with some PHP code. I want to put in an include()
      >> function so I can include existing HTML code and output it to the
      >> browser.
      >>
      >> Lo and behold PHP does not support relative paths with the include()
      >> function![/color]
      >
      > It does, works perfectly here in PHP4 and PHP5.
      > Search your code for errors.
      >
      > Grtz,
      >
      > Rik
      >
      >
      >[/color]

      No, it does not work.

      I have a file in the directory of the currently executing php code. One
      would assume the following would work:

      include 'myfile.hmtl';

      But it does not work.

      I'm using PHP5 on WinXP and Apache 2.

      Comment

      • J.O. Aho

        #4
        Re: Using Relative Paths With include()

        MBS wrote:[color=blue]
        > I am playing around with some PHP code. I want to put in an include()
        > function so I can include existing HTML code and output it to the browser.
        >
        > Lo and behold PHP does not support relative paths with the include()
        > function! (How shortsighted can you get?) Is there any way at all to use
        > relative paths with include()? Any hacks? If I use an absolute filepath,
        > everything is fine. But I don't want to do that--I can't do that. I want
        > to use the filepath relative to the currently executing PHP file.[/color]

        include("../some/other/directory/the.www.html");

        This works as long as as you don't make an include from an included file, as
        the path is always counted from the first page.

        if you have the file index.php that includes the page includes/another.php,
        which in it's turn want to include yetanother.html , you still have to include
        it as if you did the include from index.php

        include in index.php:
        include('includ es/another.php');

        include in another.php:
        include('includ es/yetanother.html ');


        //Aho

        Comment

        • MBS

          #5
          Re: Using Relative Paths With include()

          "J.O. Aho" <user@example.n et> wrote in
          news:3u0mikFuo1 hoU1@individual .net:
          [color=blue]
          > MBS wrote:[color=green]
          >> I am playing around with some PHP code. I want to put in an
          >> include() function so I can include existing HTML code and output it
          >> to the browser.
          >>
          >> Lo and behold PHP does not support relative paths with the include()
          >> function! (How shortsighted can you get?) Is there any way at all
          >> to use relative paths with include()? Any hacks? If I use an
          >> absolute filepath, everything is fine. But I don't want to do
          >> that--I can't do that. I want to use the filepath relative to the
          >> currently executing PHP file.[/color]
          >
          > include("../some/other/directory/the.www.html");
          >
          > This works as long as as you don't make an include from an included
          > file, as the path is always counted from the first page.[/color]

          Ah! There is the problem.

          Just now I noticed that when I check __FILE__ in the PHP code in question
          it does not return the current file but returns one of the includees.

          Is there anyway to get the name and directory of the currently executing
          file regardless of whether or not it has been included in another?
          Because if you have multiple files including currentfile.php , you're
          going to need to tweak your include() statement for each one unless there
          is some way to work around that.

          Thank you for the information!
          [color=blue]
          >
          > if you have the file index.php that includes the page
          > includes/another.php, which in it's turn want to include
          > yetanother.html , you still have to include it as if you did the
          > include from index.php
          >
          > include in index.php:
          > include('includ es/another.php');
          >
          > include in another.php:
          > include('includ es/yetanother.html ');
          >
          >
          > //Aho
          >[/color]

          Comment

          • Jerry Stuckle

            #6
            Re: Using Relative Paths With include()

            MBS wrote:[color=blue]
            > "J.O. Aho" <user@example.n et> wrote in
            > news:3u0mikFuo1 hoU1@individual .net:
            >
            >[color=green]
            >>MBS wrote:
            >>[color=darkred]
            >>>I am playing around with some PHP code. I want to put in an
            >>>include() function so I can include existing HTML code and output it
            >>>to the browser.
            >>>
            >>>Lo and behold PHP does not support relative paths with the include()
            >>>function! (How shortsighted can you get?) Is there any way at all
            >>>to use relative paths with include()? Any hacks? If I use an
            >>>absolute filepath, everything is fine. But I don't want to do
            >>>that--I can't do that. I want to use the filepath relative to the
            >>>currently executing PHP file.[/color]
            >>
            >>include("../some/other/directory/the.www.html");
            >>
            >>This works as long as as you don't make an include from an included
            >>file, as the path is always counted from the first page.[/color]
            >
            >
            > Ah! There is the problem.
            >
            > Just now I noticed that when I check __FILE__ in the PHP code in question
            > it does not return the current file but returns one of the includees.
            >
            > Is there anyway to get the name and directory of the currently executing
            > file regardless of whether or not it has been included in another?
            > Because if you have multiple files including currentfile.php , you're
            > going to need to tweak your include() statement for each one unless there
            > is some way to work around that.
            >
            > Thank you for the information!
            >
            >[color=green]
            >>if you have the file index.php that includes the page
            >>includes/another.php, which in it's turn want to include
            >>yetanother.ht ml, you still have to include it as if you did the
            >>include from index.php
            >>
            >>include in index.php:
            >>include('incl udes/another.php');
            >>
            >>include in another.php:
            >>include('incl udes/yetanother.html ');
            >>
            >>
            >> //Aho
            >>[/color]
            >
            >[/color]

            On Apache:

            include ($_SERVER['DOCUMENT_ROOT']) . "/relative/to/root/directory.html"

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Etienne Marais

              #7
              Re: Using Relative Paths With include()

              > Is there anyway to get the name and directory of the currently executing[color=blue]
              > file regardless of whether or not it has been included in another?
              > Because if you have multiple files including currentfile.php , you're
              > going to need to tweak your include() statement for each one unless there
              > is some way to work around that.[/color]

              For larger projets I start with index.php in the topmost directory, and
              have a directory structure of the like:

              ../include
              ../include/extension1/
              ../include/extension2/
              ../include/extra_lib1/

              ../dta
              ../dta/excel_data.xls
              ../dta/xml_data.xls

              index.php simply sets the include path:

              ini_set("includ e_path",get_inc lude_path().":. :./include/:./include/extension1:./inc.....");

              If you use some code over and over, say include/extra_lib1,
              move it out of the project tree up into the document root
              and edit php.ini to point to that directory.

              Since I have been coding OO (before PHP 5) entry into the
              system is ALLWAYS via index.php so the problem you mention
              is not applicable to me. Even without OO you can branch
              your code from index.php so that you don't load sub_program_par t1a.php
              into the url, but include/require as needed, so the paths are
              allways set and you just include what you need without including
              the directory in the include/require statement.

              If you need to find info about the main running script, check out:

              $_SERVER["REQUEST_UR I"]
              $_SERVER["SCRIPT_NAM E"]
              $_SERVER["PHP_SELF"]

              to find info about the current working directory (the directory relative
              from where anything needs to be included, regardless the running script)
              try)

              $cwd=`pwd`; // Linux
              $cwd=exec("dir" ); // or something like this for windows.

              I'm shure there must be something to find information about the
              'running sub script' but I don't know what it is :(

              Comment

              • Sandman

                #8
                Re: Using Relative Paths With include()

                In article <437b23d2$0$279 41$892e7fe2@aut hen.yellow.read freenews.net>,
                MBS <mbs@mbs.net> wrote:
                [color=blue]
                > "Rik" <luiheidsgoeroe @hotmail.com> wrote in news:dlf7km$6fa $1
                > @netlx020.civ.u twente.nl:
                >[color=green]
                > > MBS wrote:[color=darkred]
                > >> I am playing around with some PHP code. I want to put in an include()
                > >> function so I can include existing HTML code and output it to the
                > >> browser.
                > >>
                > >> Lo and behold PHP does not support relative paths with the include()
                > >> function![/color]
                > >
                > > It does, works perfectly here in PHP4 and PHP5.
                > > Search your code for errors.
                > >
                > > Grtz,
                > >
                > > Rik
                > >
                > >
                > >[/color]
                >
                > No, it does not work.
                >
                > I have a file in the directory of the currently executing php code. One
                > would assume the following would work:
                >
                > include 'myfile.hmtl';
                >
                > But it does not work.
                >
                > I'm using PHP5 on WinXP and Apache 2.[/color]

                If that's cut'n'paste from your script, then you have a typo in the extension.

                Other than that, PHP can be set not to use "." for include dir - check your
                php.ini for your include dir. Even then, files earlier in the include path
                supersedes later ones, so if you have a "myfile.htm l" earlier in the paths it
                will be included instead of the relative one.



                --
                Sandman[.net]

                Comment

                • MBS

                  #9
                  Re: Using Relative Paths With include()

                  Sandman <mr@sandman.net > wrote in
                  news:mr-FB0161.16223016 112005@individu al.net:
                  [color=blue]
                  > In article <437b23d2$0$279 41$892e7fe2@aut hen.yellow.read freenews.net>,
                  > MBS <mbs@mbs.net> wrote:
                  >[color=green]
                  >> "Rik" <luiheidsgoeroe @hotmail.com> wrote in news:dlf7km$6fa $1
                  >> @netlx020.civ.u twente.nl:
                  >>[color=darkred]
                  >> > MBS wrote:
                  >> >> I am playing around with some PHP code. I want to put in an
                  >> >> include() function so I can include existing HTML code and output
                  >> >> it to the browser.
                  >> >>
                  >> >> Lo and behold PHP does not support relative paths with the
                  >> >> include() function!
                  >> >
                  >> > It does, works perfectly here in PHP4 and PHP5.
                  >> > Search your code for errors.
                  >> >
                  >> > Grtz,
                  >> >
                  >> > Rik
                  >> >
                  >> >
                  >> >[/color]
                  >>
                  >> No, it does not work.
                  >>
                  >> I have a file in the directory of the currently executing php code.
                  >> One would assume the following would work:
                  >>
                  >> include 'myfile.hmtl';
                  >>
                  >> But it does not work.
                  >>
                  >> I'm using PHP5 on WinXP and Apache 2.[/color]
                  >
                  > If that's cut'n'paste from your script, then you have a typo in the
                  > extension.
                  >
                  > Other than that, PHP can be set not to use "." for include dir - check
                  > your php.ini for your include dir. Even then, files earlier in the
                  > include path supersedes later ones, so if you have a "myfile.htm l"
                  > earlier in the paths it will be included instead of the relative one.
                  >
                  >
                  >[/color]

                  Well, what I've got is:

                  echo 'HTML code'
                  include 'myfile.html'
                  echo 'more HTML code'

                  It works just fine with an absolute filepath. It just doesn't work with
                  the relative one.

                  As someone stated, it must be because the file I have the include() in is
                  included by anothe file and I must base the filepath off of the first
                  file.

                  Thank you and thank EVERYONE for the help. I appreciate it.

                  MBS

                  Comment

                  • Savut

                    #10
                    Re: Using Relative Paths With include()

                    I always use dirname(__file_ _) which will return the current path to
                    this file, even if it is included on another one. You never get wrong
                    with this one.

                    Comment

                    • guitarromantic@gmail.com

                      #11
                      Re: Using Relative Paths With include()

                      Would that fix my issues? The files i include are in root, all the
                      files that use it (besides index) are in subdirs off root, so i can
                      make references like include '../config.php'; except for my index.php,
                      which i had to place in /home/ and use a redirect for.

                      Comment

                      • Etienne Marais

                        #12
                        Re: Using Relative Paths With include()

                        Savut wrote:
                        [color=blue]
                        > I always use dirname(__file_ _) which will return the current path to
                        > this file, even if it is included on another one. You never get wrong
                        > with this one.[/color]

                        I whish I knew this ages ago, thanks :)

                        Etienne Marais

                        Comment

                        Working...