SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast Locker's Avatar
    Join Date
    May 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a projects paths...

    When creating a "web app" to be installed in different environments, how do you go about accommodating for different file paths? For example, a script could be installed in the root folder of "mydomain.com" or in a sub folder (mydomain.com/subfolder/) - maybe even a sub folder of greater depth (mydomain.com/subfolder/subfolder/).

    Whilst something like:
    Code PHP:
    require($_SERVER['DOCUMENT_ROOT'] . "/file.php");
    Would work in the first instance, it obviously wouldn't if installed in sub folders. I originally thought "ah, you can just create a file which stores the installation path", but then realised that that too would need to be included.

    Guidance would be appreciated

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Can't you use relative paths? That way you don't need to know the document root, nor in which subfolder the script has been installed.
    PHP Code:
    require "file.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
    Can't you use relative paths? That way you don't need to know the document root, nor in which subfolder the script has been installed.
    PHP Code:
    require "file.php"
    The problem is that the web application itself will use directories (such as /admin/).

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    But you'll know the directory structure of your own application I hope?
    So you'll be able to do things like
    PHP Code:
    require 'admin/file.php';
    require 
    '../admin/file.php'

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido - that doesn't fully solve the problem, because if there are reusable components that include other components they may not where they are being called from.

    I sometimes use something like this in a central configuration file:
    PHP Code:
    set_include_path($_SERVER['DOCUMENT_ROOT'] . '/my_app_root/' PATH_SEPARATOR get_include_path()); 
    It's still hard coded, but only once. If you wanted to make it simpler for others who install your app you could probably have some path detection code in your config file to do this automatically.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've taken to sticking things that need to be included in /lib or similar and just recursively adding all subfolders to the path.
    PHP Code:
    function rSetPath($dir) {

        if(
    is_dir($dir)) {

            
    ini_set('include_path'ini_get('include_path').PATH_SEPARATOR.$dir);

            
    $d dir($dir);
            while (
    false !== ($entry $d->read())) {

                if(
    $entry != '.' && $entry != '..') {

                    
    $entry $dir.'/'.$entry;

                    if(
    is_dir($entry)) { 
                        
    rSetPath($entry);
                    }
                }
            }
            
    $d->close();
        }


  7. #7
    SitePoint Enthusiast Locker's Avatar
    Join Date
    May 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    guido - that doesn't fully solve the problem, because if there are reusable components that include other components they may not where they are being called from.

    I sometimes use something like this in a central configuration file:
    PHP Code:
    set_include_path($_SERVER['DOCUMENT_ROOT'] . '/my_app_root/' PATH_SEPARATOR get_include_path()); 
    It's still hard coded, but only once. If you wanted to make it simpler for others who install your app you could probably have some path detection code in your config file to do this automatically.
    Wouldn't that line have to be placed at the start of each file though?

  8. #8
    SitePoint Member
    Join Date
    May 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think so too

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can include it using a relative path. This should be okay because it's likely the relative path from the main calling code is known. Subsequent scripts included don't need to be aware of where they are.


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
  •