Getting an included PHP script to to include a file in the same directory.

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

    Getting an included PHP script to to include a file in the same directory.

    Hi everyone.

    I am a little stumped at why when I try to include a file in same
    directory as the script being processed, it looks in the same directory
    as the script that included it to begin with. Is there any way I can
    get around this? Is there anyway to get the name and/or of the script
    being processed, rather than the name and/or path of the script that
    was first called?

    Many thanks.

    Daz.

  • william

    #2
    Re: Getting an included PHP script to to include a file in the samedirectory.

    On Tue, 09 Jan 2007 06:58:56 -0800, Daz wrote:
    Hi everyone.
    >
    I am a little stumped at why when I try to include a file in same
    directory as the script being processed, it looks in the same directory
    as the script that included it to begin with. Is there any way I can
    get around this? Is there anyway to get the name and/or of the script
    being processed, rather than the name and/or path of the script that
    was first called?
    in order to avoid that you might consider to define a var
    define(FS_DIR,'/path/to/you/www/project')

    just a work around

    Comment

    • Toby Inkster

      #3
      Re: Getting an included PHP script to to include a file in the same directory.

      Daz wrote:
      I am a little stumped at why when I try to include a file in same
      directory as the script being processed, it looks in the same directory
      as the script that included it to begin with. Is there any way I can
      get around this?
      include dirname(__FILE_ _).'/myfile.php';

      However, it's a good idea to have a specific "includes" directory, put all
      your included files into there and then just refer to them as:

      include 'myfile.php';

      without including a directory path at all(!) but instead using the
      include_path setting to specify where include files will be found.



      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • Michael Fesser

        #4
        Re: Getting an included PHP script to to include a file in the same directory.

        ..oO(Toby Inkster)
        >However, it's a good idea to have a specific "includes" directory, put all
        >your included files into there and then just refer to them as:
        >
        > include 'myfile.php';
        >
        >without including a directory path at all(!) but instead using the
        >include_path setting to specify where include files will be found.
        I prefer absolute paths, so I usually define a constant which contains
        the path to my include files, based on $_SERVER['DOCUMENT_ROOT'].

        Micha

        Comment

        • Jerry Stuckle

          #5
          Re: Getting an included PHP script to to include a file in the samedirectory.

          Daz wrote:
          Hi everyone.
          >
          I am a little stumped at why when I try to include a file in same
          directory as the script being processed, it looks in the same directory
          as the script that included it to begin with. Is there any way I can
          get around this? Is there anyway to get the name and/or of the script
          being processed, rather than the name and/or path of the script that
          was first called?
          >
          Many thanks.
          >
          Daz.
          >
          Or, just always use the absolute path:

          include $_SERVER['DOCUMENT_ROOT'] . '/path/filename.ext';

          $_SERVER['DOCUMENT_ROOT'] will always reference the root directory of
          your web server.

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

          Comment

          • Daz

            #6
            Re: Getting an included PHP script to to include a file in the same directory.

            Thanks William.

            I had contemplated doing something similar, but it won't work due to my
            directory structure. Yes, I know that some people would say I should
            make it work, but my directory structure is deliberately different due
            to the way I am creating my new site, which will be completely Ajax
            Powered and Ajax modules that I have written. The directory stucture
            has been thought out in order to allow consistency, and semi-automation
            (like auto detection of new modules, etc...)

            I know that many people will think it's a bad idea, but due to the
            nature of my site, it doesn't matter as only the front page will need
            to be detected by any search engines. I am hoping to give my users an
            experience like never before on the web.

            Many thanks for your input, however.

            william wrote:
            On Tue, 09 Jan 2007 06:58:56 -0800, Daz wrote:
            >
            Hi everyone.

            I am a little stumped at why when I try to include a file in same
            directory as the script being processed, it looks in the same directory
            as the script that included it to begin with. Is there any way I can
            get around this? Is there anyway to get the name and/or of the script
            being processed, rather than the name and/or path of the script that
            was first called?
            >
            in order to avoid that you might consider to define a var
            define(FS_DIR,'/path/to/you/www/project')
            >
            just a work around

            Comment

            • Daz

              #7
              Re: Getting an included PHP script to to include a file in the same directory.


              Toby Inkster wrote:
              Daz wrote:
              >
              I am a little stumped at why when I try to include a file in same
              directory as the script being processed, it looks in the same directory
              as the script that included it to begin with. Is there any way I can
              get around this?
              >
              include dirname(__FILE_ _).'/myfile.php';
              >
              However, it's a good idea to have a specific "includes" directory, put all
              your included files into there and then just refer to them as:
              >
              include 'myfile.php';
              >
              without including a directory path at all(!) but instead using the
              include_path setting to specify where include files will be found.
              >

              >
              --
              Toby A Inkster BSc (Hons) ARCS
              Hi Toby.

              Thanks for that, I think that's the solution I am looking for. The way
              my website is designed, is so that everything goes through the main
              index.php file, and any core files (user functions, database functions,
              cookie functions etc...), are all included by the main index.php file,
              which validates the user, and then loads the correct module according
              to the search parameter in the URL. I know that there aren't many
              websites that do this, but after much careful thought I decided that it
              would make more sense as I won't have to write so many repeated lines
              of code, thus taking up less space on the server, and hopefully saving
              mistakes like missing code/core functions.

              Many thanks again, and all the best. :)

              Daz.

              Comment

              • Daz

                #8
                Re: Getting an included PHP script to to include a file in the same directory.


                Michael Fesser wrote:
                .oO(Toby Inkster)
                >
                However, it's a good idea to have a specific "includes" directory, put all
                your included files into there and then just refer to them as:

                include 'myfile.php';

                without including a directory path at all(!) but instead using the
                include_path setting to specify where include files will be found.
                >
                I prefer absolute paths, so I usually define a constant which contains
                the path to my include files, based on $_SERVER['DOCUMENT_ROOT'].
                >
                Micha
                I know there you are coming from, although I prefer relative, mainly
                because my modules then become portable and self-contained. If I decide
                to build a new site at any point with a different structure but using
                the same modules, the include paths won't need to be changed.

                All the best.

                Daz.

                Comment

                • boclair

                  #9
                  Re: Getting an included PHP script to to include a file in the samedirectory.

                  Daz wrote:
                  Michael Fesser wrote:
                  >
                  >.oO(Toby Inkster)
                  >>
                  >>However, it's a good idea to have a specific "includes" directory, put all
                  >>your included files into there and then just refer to them as:
                  >>>
                  >> include 'myfile.php';
                  >>>
                  >>without including a directory path at all(!) but instead using the
                  >>include_pat h setting to specify where include files will be found.
                  >I prefer absolute paths, so I usually define a constant which contains
                  >the path to my include files, based on $_SERVER['DOCUMENT_ROOT'].
                  >>
                  >Micha
                  >
                  I prefer relative, mainly echo because my modules then become portable and self-contained
                  Just a suggestion. How about concating $_SERVER['HTTP_HOST'] and
                  $_SERVER['SCRIPT_NAME'] to provide the absolute URL on whatever the
                  server. Useful for header() re-directs.

                  Louise


                  Comment

                  • Toby Inkster

                    #10
                    Re: Getting an included PHP script to to include a file in the same directory.

                    boclair wrote:
                    Just a suggestion. How about concating $_SERVER['HTTP_HOST'] and
                    $_SERVER['SCRIPT_NAME'] to provide the absolute URL on whatever the
                    server. Useful for header() re-directs.
                    I think Daz was referring to (server-side) paths, not (web-side) URLs.

                    --
                    Toby A Inkster BSc (Hons) ARCS
                    Contact Me ~ http://tobyinkster.co.uk/contact

                    Comment

                    Working...