SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy How to manage paths correctly?

    I want to learn how to manage paths correctly.

    I have a file 'paths.php' inside /home/mysite/public_html with constants containing the relative paths to several files and directories.

    The problem is that paths.php is only good if included from a file inside the public_html directory, if paths.php is included from the includes directory all the paths are wrong because the paths are relative to the script that calls the include.

    Do I need to create a paths file in every directory??

    Please, help me.

  2. #2
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use absolute paths.

    In PHP < 5.3 use:

    PHP Code:
    dirname(__FILE__) . '/some_file.php'
    In PHP >= 5.3 use:

    PHP Code:
    __DIR__ '/some_file.php'
    I'm under construction | http://igstan.ro/

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    I think I cannot use absolute paths in this case because the includes folder is located outside of the public_html directory

    /home/mysite/includes <-includes directory
    /home/mysite/public_html

  4. #4
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you say it wouldn't work?
    I'm under construction | http://igstan.ro/

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you are using relative paths, you can use getcwd() (get current working directory) to determine which working directory the relative paths will be from. If you are in the includes directory, you can prepend a relative path to all your paths in the script to get them to the public directory. If you are in the public html directory, you can prepend a relative path to get you to the includes directory.

    The current working directory should be the folder containing the script that first executed; unless you change it. I run everything off index.php (and use .htaccess to create the illusion of SEF urls). That way my current working directory is always the root html folder. But I am using a rather complicated CMS framework too.

    Edit: after I said all that, I realized I could have just said Amenthes is right. It would be easier to just prepend Amenthes's solution to your relative paths. All the relative paths should be based off that path that contains the paths php file.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    Norway
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To include a file relative to the file you're writing in:

    PHP Code:
    require realpath(dirname(__FILE__) . '/../somefolder/somefile.php'); 
    (You can do without the realpath() if you're not using '../' in your path.)

    To include a file realtive to the file that's in your url:

    require '/somefolder/somefile.php';

    You can also set up include paths, with the set_include_path function. This will make php look for files/folders in your include path as well as relative to the active file.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got confused thinking I couldn't use absolute paths to the includes directory because this directory is not inside the public folder. Sorry.

    Would this be the correct syntax to define an absolute path?

    PHP Code:
    /*  path.php  */ 
    define(PUBLIC_BASE_PATH'/home/mysite/public_html'); 
    and to include the file:


    PHP Code:
    /*  other files  */
    include("/home/mysite/public_html/path.php");
    include(
    PUBLIC_BASE_PATH.'init.php'); 

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Yes, that would work (as long as you make sure you have the correct slashes in the paths).

    PHP Code:
    include(PUBLIC_BASE_PATH.'/init.php'); 
    // see the extra slash before init.php 
    PHP Code:
    define(PUBLIC_BASE_PATH'/home/mysite/public_html'); 
    As suggested earlier, the above could also be written like so:

    PHP Code:
    define(PUBLIC_BASE_PATHdirname(__FILE__)); 
    This way you don't have to change the path when uploading changes or moving to a different host.

  9. #9
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this:
    Quote Originally Posted by Amenthes View Post
    Use absolute paths.

    In PHP < 5.3 use:

    PHP Code:
    dirname(__FILE__) . '/some_file.php'
    In PHP >= 5.3 use:

    PHP Code:
    __DIR__ '/some_file.php'
    The problem with your define solutions is that you STILL need to hard-code the path to that first file that has the define

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been trying the paths with __DIR__ and it is very convenient. Thanks a lot!

    The problem with your define solutions is that you STILL need to hard-code the path to that first file that has the define
    I don't really understand what you mean. If I am including at the includes directory and want to find the paths.php file in public_html, can I do so with __DIR__?

    PHP Code:
    /* file in includes directory  */
    echo __DIR__// will return -> /home/mysite/includes  (I'm not 100% sure of this, as I've been testing in localhost 
    When the file I need to access is at this following path:
    PHP Code:
    /home/mysite/public_html/paths.php 

  11. #11
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    db.inc.php
    PHP Code:
    define(PUBLIC_BASE_PATH'/home/mysite/public_html'); 
    foo.php
    PHP Code:
    include 'db.inc.php';
    echo 
    "My path is: " PUBLIC_BASE_PATH
    That will work, BUT if you move/include foo.php in another script, then you need to change it's include path.
    If you write:
    PHP Code:
    include __DIR__ '/db.inc.php';
    echo 
    "My path is: " PUBLIC_BASE_PATH
    Then, your PUBLIC_BASE_PATH is useless because you already know the path (__DIR__ + relative path = absolute path.)

    So, you can just use __DIR__

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry this is so complicated for me.

    In the paths php file I changed all the paths to start with __DIR__

    Edit: after I said all that, I realized I could have just said Amenthes is right. It would be easier to just prepend Amenthes's solution to your relative paths. All the relative paths should be based off that path that contains the paths php file.
    However, then I also used the getcwd method at the "first line" php file (that is included in all files at the beginning) to find the current directory and then used a switch statement to indicate the correct path to 'paths.php' from any directory in the site.

    I don't really understand how else to include the paths.php from any directory.

    This seems like it may be a bad idea though, what do you think?

  13. #13
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    PHP Code:
    echo __DIR__
    See what that gets you, and you will see that you don't need getcwd.

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've read the thread over a hundred times to see if I'm missing something. Forgive me and thanks for your patience.

    I understand why I should use __DIR__ in the paths file, and I changed all the paths to this format.

    However, where I get lost is that from any file I still need to include the paths file. If the paths file is one directory above, how do I use __DIR__ to link to the paths file? And why is it better to use __DIR__ as you tell me in this case?

  15. #15
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    However, where I get lost is that from any file I still need to include the paths file.
    Just make sure any script called by the web server (index.php etc) knows where paths.php is:
    include '../paths.php';
    Then you're nested includes can use these paths and be happy.


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
  •