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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How Does PHP Determine Require File Relative Path?

    Hi,
    I have a require('file.php'); how does php determine the relative path of file.php? Is it the same path of the file that has the require() or is it the root of the web server like /var/www/html?

    Many thanks.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Relative path means a path relative to the file. So if in example.php you put require('file.php');, it would include file.php from the same directory as example.php. 'foo/file.php' would be file.php in the foo directory inside the example.php's directory. '../file.php' would be file.php in the parent directory to example.php's directory.

    Example:

    /home/user/public_html/index.php includes 'includes/header.php' PHP would be including /home/user/public_html/includes/header.php

    Or if /home/user/public_html/index.php includes '../header.php' then PHP would be including /home/user/header.php.

    Of course, PHP doesn't write it out like this. It executes a filesystem call and it determines what file ../header.php is.
    "$question = ( to() ) ? be() : ~be();"

  • #3
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Note that the relative starting point is always from the file being executed. Ie., if you have:

    /include.php
    /myapp/index.php
    /myapp/otherinclude.php

    if index.php (the file the users requests) includes '../include.php', include.php must include otherinclude as 'otherinclude.php' and not 'myapp/otherinclude.php'

    For this reason if you have includes spanning several directories or includes that are used in different contexts, it's often easier to define a root path and include absolutely.

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I myself move all my critical code to a web-inaccessable directory, then in their stead have files which require the files they need to function. More for security than anything, as once when I recompiled Apache I forgot to include PHP in there and all my code was displayed for the world to see. This way, all people see is a few requires and echos, not the actual code were it to happen again.
    "$question = ( to() ) ? be() : ~be();"

  • #5
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I don't quite understand what you said. Did you mean to move all files besides ones that's actually on the url to outside of the web directory? If so, will I get permission error when I require them in the php files? How exactly do you do this?

    I heard that people can view php script if they pull the network cable during webpage loading.

    Quote Originally Posted by Velox Letum
    I myself move all my critical code to a web-inaccessable directory, then in their stead have files which require the files they need to function. More for security than anything, as once when I recompiled Apache I forgot to include PHP in there and all my code was displayed for the world to see. This way, all people see is a few requires and echos, not the actual code were it to happen again.
    Last edited by dealmaker; 12-11-2005 at 08:01 AM.

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, PHP includes files through the filesystem, not through the web, which is why it works. And whoever told you that stopping a page in transaction would allow you to see the code doesn't know what they're talking about. All PHP is executed before the page is even sent to the client.

    You don't need to (or probably want to) move your critical PHP files to outside of the webroot, it's just my inherent paranoia. The only reason I do it really is because I'm always recompiling Apache and PHP to test new extensions and such. Ah, the perks of your own server...
    "$question = ( to() ) ? be() : ~be();"

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Note that PHP also looks in its include_path as set in the php.ini (or in httpd conf or can be changed at runtime with ini_set())

    so with include('file.php') php first looks in the current working directroy , if it does not find it there is checks for the file in the include_path (first in, best dressed if multiple paths)

    I tend to use absolute paths anyway so not an issue for me.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder
    Join Date
    Nov 2003
    Location
    Minneapolis, MN
    Posts
    2,879
    Thanks
    2
    Thanked 65 Times in 56 Posts
    Quote Originally Posted by firepages
    I tend to use absolute paths anyway so not an issue for me.
    Forgive my noobishness, but does that mean you are using "/path/from/server/to/domain/file.php" for every include? I use includes a lot and was hoping there was some way to set the domain as the root rather than the server, as in HTML.

    I know, you could use "<?php include($DOCUMENT_ROOT . "/includes/file.php"); ?>" but it's not like anyone's saving any typing.

  • #9
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    that's exactly my problem. I have 'include' directory for classes. Those files have the same name as the ones that 'require' the files inside 'include'. There are also templates directory. So there can be a chain of files in different directories chained by the 'require' functions. If I use relative path, it will certainly create problems when PHP tries to find the files. I am trying to solve that without using absolute path. How?

    Quote Originally Posted by ralph l mayo
    For this reason if you have includes spanning several directories or includes that are used in different contexts, it's often easier to define a root path and include absolutely.

  • #10
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well let me answer a question there. Say you have a structure like this:

    index.php
    -includes
    includes/blah.php
    which includes
    includes/blah2.php

    If index.php includes blah.php, then you include blah2.php from blah.php you need to include 'includes/blah2.php', as it determines the relative path from where it was included.
    "$question = ( to() ) ? be() : ~be();"

  • #11
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    yes, I understood this part. But most likely, I have situation when there is another file index2.php in a different directory than index.php and it include includes/blah.php as well.
    Now, PHP won't find blah2.php from index2.php.

    Quote Originally Posted by Velox Letum
    Well let me answer a question there. Say you have a structure like this:

    index.php
    -includes
    includes/blah.php
    which includes
    includes/blah2.php

    If index.php includes blah.php, then you include blah2.php from blah.php you need to include 'includes/blah2.php', as it determines the relative path from where it was included.

  • #12
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could just use absolute paths, ie:

    /home/user/public_html/includes/blah2.php

    Then you wouldn't have to worry about it.
    "$question = ( to() ) ? be() : ~be();"

  • #13
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Quote Originally Posted by rmedek
    Forgive my noobishness, but does that mean you are using "/path/from/server/to/domain/file.php" for every include? I use includes a lot and was hoping there was some way to set the domain as the root rather than the server, as in HTML.

    I know, you could use "<?php include($DOCUMENT_ROOT . "/includes/file.php"); ?>" but it's not like anyone's saving any typing.

    well nearly , I use
    <?php include_once FP_FILE_ROOT.'/path/to/file';?>
    where FP_FILE_ROOT has been previously defined as /home/httpd/etc in a globally included file (which sadly has to be included via a relative path else there is no point )

    it saves little typing today you are correct , but if you move to a different host or change path then you have only to edit the 1 file that defines that path.

    also I develop locally on windows and test on my local backup linux box & a simple conditional lets the code run on all 3 without any changes at all.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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