include() and path resolution

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

    include() and path resolution

    Hi All,

    I have a PHP program that includes files from the 'include' directory
    relative to the path that translates to the HTTP root of the site.
    This is '/var/www/html' on the server, and '~/username/public_html' on
    my dev box.

    My question is - is there any way to resolve the path to a file,
    something like:

    include GetBasePath() . '/include/top.inc';

    where GetBasePath() returns '/var/www/html' or
    '~/username/public_html' depending upon which server it's running?
    I've looked through the manual & the FAQ but found nothing.

    TIA for any suggestions.

    Yours,
    Duncan Bayne
  • Chris Hope

    #2
    Re: include() and path resolution

    Duncan Bayne wrote:
    [color=blue]
    > I have a PHP program that includes files from the 'include' directory
    > relative to the path that translates to the HTTP root of the site.
    > This is '/var/www/html' on the server, and '~/username/public_html' on
    > my dev box.
    >
    > My question is - is there any way to resolve the path to a file,
    > something like:
    >
    > include GetBasePath() . '/include/top.inc';
    >
    > where GetBasePath() returns '/var/www/html' or
    > '~/username/public_html' depending upon which server it's running?
    > I've looked through the manual & the FAQ but found nothing.[/color]

    You could try using
    dirname(__FILE_ _)
    That will give you the absolute path to the directory of the file you call
    it in.

    If you wanted to include the file '../somedir/somefile.php' relative to the
    current file you could do
    include(dirname (__FILE__) . '../somedir/somefile.php')

    --
    Chris Hope
    The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Virgil Green

      #3
      Re: include() and path resolution

      "Duncan Bayne" <dhbayne@ihug.c o.nz> wrote in message
      news:f8f9c724.0 405161805.2d96a ec@posting.goog le.com...[color=blue]
      > Hi All,
      >
      > I have a PHP program that includes files from the 'include' directory
      > relative to the path that translates to the HTTP root of the site.
      > This is '/var/www/html' on the server, and '~/username/public_html' on
      > my dev box.
      >
      > My question is - is there any way to resolve the path to a file,
      > something like:
      >
      > include GetBasePath() . '/include/top.inc';
      >
      > where GetBasePath() returns '/var/www/html' or
      > '~/username/public_html' depending upon which server it's running?
      > I've looked through the manual & the FAQ but found nothing.
      >[/color]

      include($_SERVE R['DOCUMENT_ROOT'] . '/include/top.inc');

      - Virgil


      Comment

      • Tony Marston

        #4
        Re: include() and path resolution

        Why do you think you need such a thing? The include_path option in the
        PHP.INI file may contain several entries, therefore it may not be possible
        to resolve it to a single path. Besides, PHP will automatically scan ALL the
        entries in the include_path when attempting to resolve an include()
        statement. You do NOT need to specify any directory name on an include()
        statement as PHP will examine all the directory names on the include_path
        setting.

        --
        Tony Marston

        This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




        "Virgil Green" <vjg@obsydian.c om> wrote in message
        news:R8sqc.952$ n63.196@newssvr 23.news.prodigy .com...[color=blue]
        > "Duncan Bayne" <dhbayne@ihug.c o.nz> wrote in message
        > news:f8f9c724.0 405161805.2d96a ec@posting.goog le.com...[color=green]
        > > Hi All,
        > >
        > > I have a PHP program that includes files from the 'include' directory
        > > relative to the path that translates to the HTTP root of the site.
        > > This is '/var/www/html' on the server, and '~/username/public_html' on
        > > my dev box.
        > >
        > > My question is - is there any way to resolve the path to a file,
        > > something like:
        > >
        > > include GetBasePath() . '/include/top.inc';
        > >
        > > where GetBasePath() returns '/var/www/html' or
        > > '~/username/public_html' depending upon which server it's running?
        > > I've looked through the manual & the FAQ but found nothing.
        > >[/color]
        >
        > include($_SERVE R['DOCUMENT_ROOT'] . '/include/top.inc');
        >
        > - Virgil
        >
        >[/color]


        Comment

        • Chris Hope

          #5
          Re: include() and path resolution

          Tony Marston wrote:
          [color=blue]
          > Why do you think you need such a thing? The include_path option in the
          > PHP.INI file may contain several entries, therefore it may not be possible
          > to resolve it to a single path. Besides, PHP will automatically scan ALL
          > the entries in the include_path when attempting to resolve an include()
          > statement. You do NOT need to specify any directory name on an include()
          > statement as PHP will examine all the directory names on the include_path
          > setting.[/color]

          That's all well and good saying that, but if you are hosting on a shared
          hosting platform and cannot alter the php.ini to your specific directory of
          includes then your advice is not much help.

          Note however that you can alter the include path in an .htaccess file,
          assuming you are on an Apache host on a Unix platform, or can use ini_set()
          to change the include path at the beginning of your scripts and then just
          call include() without a path as Tony here suggests.

          If you do have to use ini_set() then you'll need to check the other posts
          about how to set it to the absolute path which comes back to your original
          question. (BTW I like Virgil's way better than I suggested!)

          Comment

          • Virgil Green

            #6
            Re: include() and path resolution


            "Tony Marston" <tony@NOSPAM.de mon.co.uk> wrote in message
            news:c8dknt$bee $1$8300dec7@new s.demon.co.uk.. .[color=blue]
            > "Virgil Green" <vjg@obsydian.c om> wrote in message
            > news:R8sqc.952$ n63.196@newssvr 23.news.prodigy .com...[color=green]
            > > "Duncan Bayne" <dhbayne@ihug.c o.nz> wrote in message
            > > news:f8f9c724.0 405161805.2d96a ec@posting.goog le.com...[color=darkred]
            > > > Hi All,
            > > >
            > > > I have a PHP program that includes files from the 'include' directory
            > > > relative to the path that translates to the HTTP root of the site.
            > > > This is '/var/www/html' on the server, and '~/username/public_html' on
            > > > my dev box.
            > > >
            > > > My question is - is there any way to resolve the path to a file,
            > > > something like:
            > > >
            > > > include GetBasePath() . '/include/top.inc';
            > > >
            > > > where GetBasePath() returns '/var/www/html' or
            > > > '~/username/public_html' depending upon which server it's running?
            > > > I've looked through the manual & the FAQ but found nothing.
            > > >[/color]
            > >
            > > include($_SERVE R['DOCUMENT_ROOT'] . '/include/top.inc');
            > >
            > > - Virgil
            > >
            > >[/color]
            > Why do you think you need such a thing? The include_path option in the
            > PHP.INI file may contain several entries, therefore it may not be possible
            > to resolve it to a single path. Besides, PHP will automatically scan ALL[/color]
            the[color=blue]
            > entries in the include_path when attempting to resolve an include()
            > statement. You do NOT need to specify any directory name on an include()
            > statement as PHP will examine all the directory names on the include_path
            > setting.[/color]

            (top-posting 'corrected' for my reading pleasure)

            I don't. The OP might. In addition to the include_path, the include will
            look in the same directory as the currently executing file.

            Personally, I prefer to have just the . in the include_path along with any
            other system-level includes I might need and then use relative filenames.
            But, as Chris pointed out, you might not always have access to the php.ini.
            You might not even be able to change include_path via .htaccess if your host
            has implemented phpsuexec (or phpsu, or whatever variant). Then, you are
            supposed to use a private php.ini... but I haven't tried or bothered. I
            still prefer relative file paths.

            - Virgil


            Comment

            Working...