Results 1 to 10 of 10

Thread: Keeping track of DIRs when Including scripts. -[RESOLVED]-

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Resolved Keeping track of DIRs when Including scripts. -[RESOLVED]-

    Whats the best way to keep track of what Directory you are in when including files.

    For example I have the script being run: /index.php
    But inside it it will include a file /inc/user.inc.php
    Then inside that file it wants to include /inc/error.inc.php

    Kinda simple apart from if I include /inc/user.inc.php into the file /admin/index.php then the line inside /inc/user.inc.php that includes the error include will be wrong, because its a relative include.

    I must use relative includes because I want this to be able to run on different servers and they may not be hosting from the same DIR.

    I had a thought of having a variable in every script that will hold which DIR it is in but didn't know if that was tacky. Also if the files were moved to another DIR this falls down. What is the easy way to determine the DIR the script is being run from relative to the Hosting DIR.

    Thanx
    Last edited by Electroman; Feb 16th, 2005 at 06:39 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Keeping track of DIRs when Including scripts.

    The best way to do it is to include everything relative to the base directory of your application. The reason I say this is becuase the default include path is the current directory of the of the file you are calling include from. If the file doesn't exist in that directories include path, it then searches the include path for the scripts current working directory.
    Last edited by visualAd; Feb 16th, 2005 at 06:12 AM. Reason: Incorrect info - see post below.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Keeping track of DIRs when Including scripts.

    thanx, but what if you had a file /admin/admin.php and it also wants to include /db/mysql.php and /classes/site.php. This is what i'm facing. I think my solution is to pass a variable which will represent how to get to the Root of the application. So in this case inside admin.php it would equal "../" but inside index.php it would be an empty string. This should work but could be a bit messy.

    My .inc.php files only ever include another file from inside functions so it is easy enough to add an extra parameter saying where the root is of the app.

    Thanx for you help though .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Keeping track of DIRs when Including scripts.

    Now I'm confused - I've just been testing this on my server and the behaviour is a bit inconsistant.


    Give us a few.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Keeping track of DIRs when Including scripts.

    I'll try to simplify what I get:

    Files:
    /index.php
    /user/staff.php
    /inc/bob.inc.php
    /inc/sue.inc.php


    File: /index.php
    include "inc/bob.inc.php";
    // More code...


    File: /inc/bob.inc.php
    include "inc/sue.inc.php"; // Expected usage from /index.php
    // More Code...


    File: /user/staff.php
    include "../inc/bob.inc.php";
    // More Code...



    Running /index.php works fine.
    Running /user/staff.php fails, it tried to find /user/inc/sue.inc.php
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Keeping track of DIRs when Including scripts.

    And my Hacky solution:

    File: /index.php
    $Root = "";
    include $Root."inc/bob.inc.php";
    // More code...


    File: /inc/bob.inc.php
    include $Root."inc/sue.inc.php";
    // More Code...


    File: /user/staff.php
    $Root = "../";
    include $Root."inc/bob.inc.php";
    // More Code...



    Both will now run fine. I guess I don't need the $Root used int eh includes for the actual scripts being run (index.php & staff.php) but I guess if I use that and check if $Root has been defined already i'd be able to include anything from everywhere and it will work.
    Last edited by Electroman; Feb 15th, 2005 at 08:12 PM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Keeping track of DIRs when Including scripts.

    After a quick look on the documentation for include on PHP.net I found this:
    Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ../, it is looked only in include_path relative to the current working directory.
    The first place when using a relative path that PHP looks is in the current directory of the actual file. If it is not found then the current working directory is then searched, this is return by getcwd(). The exception as explained above is with the relative path ../. If use this then only the current working directory search.

    Does your staff.php file change the current working directory at any point during execution. If not then I am not sure why its not working.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: Keeping track of DIRs when Including scripts.

    Quote Originally Posted by Electroman
    And my Hacky solution:

    File: /index.php
    $Root = "";
    include $Root."inc/bob.inc.php";
    // More code...


    File: /inc/bob.inc.php
    include $Root."inc/sue.inc.php";
    // More Code...


    File: /user/staff.php
    $Root = "../";
    include $Root."inc/bob.inc.php";
    // More Code...



    Both will now run fine. I guess I don't need the $Root used int eh includes for the actual scripts being run (index.php & staff.php) but I guess if I use that and check if $Root has been defined already i'd be able to include anything from everywhere and it will work.
    Thats how I'm doing it.
    Don't Rate my posts.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Keeping track of DIRs when Including scripts. -[RESOLVED]-

    I'm not a big fan of using variables to keep track of the directory. Keep it all well organised and you shouldn't have a problem.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Keeping track of DIRs when Including scripts. -[RESOLVED]-

    BTW from what visual posted if I just put include "sue.inc.php"; in bob.inc.php then it actually works. Seems a little strange but using ../ will make it switch to only search relative to the CWD (where the script being run is). Whereas if you just write a relative address without any ../ in then it will first search where the file the code is in (in this case the inc folder) then the CWD after that if its not found. Pretty annoying if I need to go back a DIR but luckily al my includes are in the same DIR
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width