Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow includes and relative paths

    Having the following, why file3A.php is being included while file3B.php is not?:
    Code:
    + FOLDER1
      - file1.php
      + FOLDER2
        + FOLDER3
          - file2.php
          + FOLDER5
            - file3A.php
        + FOLDER4
          -file3B.php
    PHP Code:
    /* file1.php: */

    require_once('FOLDER2/FOLDER3/file2.php'); // ok

    /* file2.php: */

    require_once('FOLDER5/file3A.php'); // right

    require_once('../FOLDER4/file3B.php'); //worng 

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    ../ Means back a directory. I have no idea where abouts you are in that hierarchy.

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire
    ../ Means back a directory. I have no idea where abouts you are in that hierarchy.
    a) Ok. b) Neither do I, just experimenting...

    From the manual:

    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.

    I just want to find out why the last statement can't include the file.

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh god, I just realized the answer was written there. Fool me...

    [...] If filename begins with ../, it is looked only in include_path relative to the current working directory. [...]

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You could play a bit with the realpath() function.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Having the same hierarchy:
    Code:
    + FOLDER1
      - file1.php
      + FOLDER2
        + FOLDER3
          - file2.php
          + FOLDER5
            - file3A.php
        + FOLDER4
          -file3B.php
    but this:
    PHP Code:
    /* file1.php */

    $var "foo";

    function 
    include_file($file_path)
    {
      require_once(
    $_SERVER['DOCUMENT_ROOT'].$file_path);
    }

    include_file('/FOLDER1/FOLDER2/FOLDER3/file2.php');

    /* file2.php */

    include_file('/FOLDER1/FOLDER2/FOLDER3/FOLDER5/file3A.php');

    include_file('/FOLDER1/FOLDER2/FOLDER4/file3B.php'); 
    I get the following:

    file1.php // $var is available and file2.php is included
    file2.php // $var is not available; $GLOBALS['var'] is available; file3A.php and file3B.php are included
    file3A.php // $var is not available; $GLOBALS['var'] is available;
    file3B.php // $var is not available; $GLOBALS['var'] is available;

    In the real application I'm not having access to the vars if not through $GLOBALS, is that because I must pass references to all set vars to the function?, if that's so, how could I go around this without that function?

    I basically need to let files have access to any file in anyway without loosing references. Thanks for replying.
    Last edited by maltrecho; 07-02-2005 at 02:02 PM.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because $var is created outside the function and the page is called from within the function it makes sense that $var would only be available to the page via $GLOBALS, since it doesn't have scope within the function otherwise. There's a reference to this behaviour on the PHP site:
    If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.
    Because include and require are language constructs and not functions, if you call the page using require_once directly instead of calling it via your function, it should inherit the normal scope of your variables:
    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.


  •  

    Posting Permissions

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