SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Porto,Portugal
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best way to deal with includes file paths

    Hi.
    I need to include a file into some other files that are in different directories.
    I have made a config_php file that contains a defined variable BASE_URI which the path to the root folder of my webiste.

    The problem is to include the config_php the relative path for the varies from page to page (pages in different folders).

    Should i use an absolute path to that file.or there is a best way?

    PS:When i try to use absolute path in localhost i get an error:
    "URL file-access is disabled in the server configuration "

    How to change that?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    include '/config.php'

  3. #3
    SitePoint Enthusiast Locker's Avatar
    Join Date
    May 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    PHP Code:
    include '/config.php'
    Surely that wont have the desired effect? For example, the path could be...

    /something/something/scriptlocation

    In this situation, it would look for config.php in the first "/" directory, rather than "scriptlocation".

    I'm having a similar problem. I want to know how to make a web app work no matter what directory it is installed in. I thought the way to get around that would be to store the path to the script installation in a config file, but then that file itself needs to be included which is where it gets confusing.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Locker View Post
    Surely that wont have the desired effect? For example, the path could be...

    /something/something/scriptlocation

    In this situation, it would look for config.php in the first "/" directory, rather than "scriptlocation".
    Of course. I was assuming his config file would be in the root directory. In your example, the include would be
    PHP Code:
    include '/something/something/config.php'
    It would be more complicated if he didn't know the exact folder, because it wouldn't be possible to hard code the path to the file. But it seemed to me from his OP that this is not the case. He tried an absolute path, and got an error. If he knows the absolute path to the file, he'll be able to know the relative path starting from the root ('/') as well.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Porto,Portugal
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didnt know that if i put /... it will start from the root directory.

    Thanks. That should work.

    I love this forum


    Edit: Strange. If i put require_once '/Site/config/config.php'; it doesn´t work.
    If i put $_SERVER['DOCUMENT_ROOT']. '/Site/config/config.php it works.

    I am using wamp and my server root is C:/wamp/www.

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Popovich88 View Post
    I didnt know that if i put /... it will start from the root directory.

    Thanks. That should work.

    I love this forum


    Edit: Strange. If i put require_once '/Site/config/config.php'; it doesn´t work.
    If i put $_SERVER['DOCUMENT_ROOT']. '/Site/config/config.php it works.

    I am using wamp and my server root is C:/wamp/www.
    It's not strange. PHP is operating on a filesystem level so absolute paths are relative to the root of the volume, not the document root.

  7. #7
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my projects, Im usually using a constant based on a file or a folder I know exists. Usually its my "Includes" folder

    This is placed in the configuration file, which ofcourse is included on every page of my website.

    The code is simple, and works like a charm:
    Code PHP:
    // locate the root folder, based on the location of the Administration folder
    for ($root = ''; !is_dir($root.'Includes'); $root .= '../');
    define('ROOT', $root);

    No matter how deep into your file structure you are, the for loop moves back through the folders, looking for the directory "Includes" and when it locates the folder, the contents of the variable "$root" is placed in the constant "ROOT".
    Because its a constant, it will work in functions aswell.

    Example of usage:
    Code PHP:
    include(ROOT.'Includes/Navigation.php');

    Ofcourse one can easily be creative, and define constants for each folder, making it easy to locate specifik folders without risking typo's

    Code PHP:
    // locate the root folder, based on the location of the Administration folder
    for ($root = ''; !is_dir($root.'Includes'); $root .= '../');
    define('ROOT', $root);
    define('ADMIN', ROOT.'Administration/');
    define('INCLUDES', ROOT.'Includes/');
    define('IMAGES', ROOT.'Images/'); 
    // etc.....

    Another neat bonus, is that I can test if ROOT is not defined, and make sure no one directly accesses any file that should have been included.
    Simply place this line at the top of the file:
    Code PHP:
    if(!defined('ROOT')) header('location: http: //www.google.com');

    The only drawback I can think of, is that I need to know the location of the configuration file where the ROOT constant is defined, but I think it's a relative easy thing to keep track of.
    zalucius

  8. #8
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zalucius View Post
    In my projects, Im usually using a constant based on a file or a folder I know exists. Usually its my "Includes" folder

    This is placed in the configuration file, which ofcourse is included on every page of my website.
    How do you include that 1 configuration file?

  9. #9
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali View Post
    How do you include that 1 configuration file?
    Just like you normally would...

    Imagine we are in the administration of the website, the page requested resides in the admin folder, giving a path like this: "Administration/edit_something.php"
    The configuration file is located in the "Includes" folder, giving it a path like this "Includes/configuration.php"

    The edit_something.php would then include the configuration like this:
    Code PHP:
    require_once('../Includes/configuration.php');
    require_once(ADMIN.'some_file_in_admin_folder.php'); // just as an example

    If the requested file was in a subfolder of a subfolder of the includes folder, then the path would look like this:
    Code PHP:
    require_once('../../configuration.php');
    require_once(IMAGES.'some_picture.jpg'); // just as an example
    zalucius


Bookmarks

Posting Permissions

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