Include path query

Discussion in 'PHP' started by ian_ok, Jun 4, 2006.

  1. #1
    The include path goes something like ./../../folder/to_incude.html depending on how many folders deep a file using the incude file is located.

    Is there a way to have this be set for all include statement so that where ever the file is the include path will always be the same.

    So if a file is located 6 folders deep the path will be the same as a file located in the root?

    Ian
     
    ian_ok, Jun 4, 2006 IP
  2. acplus

    acplus Guest

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi Ian,

    Yes this is possible, but it would be a security issue. You want to include a PHP script starting at the top of the folder hierarchy then search through the sub-folders until the include file has been found. This could be a security issue as you never know which one it is going to include as "someone" may place an include file in the root folder which does malicious activities.

    You'll need a function which uses recursion to scan through the entire directories starting at the root and then iterate through the files and folders within the current folder.

    I think you'll find the following pseudo code helpful:

    searchDirectory(mydirectory);

    function searchDirectory(mydirectory)
    for each file in directory
    if file is include file then
    include_once file
    exit
    end if
    end for
    for each file in directory
    if file is directory then
    searchDirectory(file)
    end if
    end for
    end function

    Theres probably a more efficient way of doing this.

    Regards,
    acplus
     
    acplus, Jun 4, 2006 IP
  3. mykoleary

    mykoleary Peon

    Messages:
    64
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    include get_ini("doc_root") . "/path/to/file/from/root";

    Will include the correct file every time no matter where in the hiearchy you may be (note, I may have misspelled the doc_root attirbute, if so, look for the correct spelling in yout php.ini file - I don't have on ehandy to verify...)
     
    mykoleary, Jun 4, 2006 IP
  4. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks very much.

    ian
     
    ian_ok, Jun 7, 2006 IP