How to get your site root via code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whizzkid1892
    New Member
    • Dec 2007
    • 5

    How to get your site root via code

    I've been following a tutorial from lynda.com (beyond the basics) - theres a page of code which defines your site root. But I cant seem to find the correct path - here is the code of initilize.php

    PHP Code:
    Code:
    <?php
    
    // Define the core paths
    // Define them as absolute paths to make sure that require_once works as expected
    
    // DIRECTORY_SEPARATOR is a PHP pre-defined constant
    // (\ for Windows, / for Unix)
    defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
    
    defined('SITE_ROOT') ? null :
    
        
    
    define('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');
    //define('SITE_ROOT', DS.'home'.DS.'sites'.DS.'mydomain.com'.DS.'public_html');  THIS IS WHAT I TRIED BUT IT DOESNT WORK
    defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
    
    // load config file first
    require_once(LIB_PATH.DS.'config.php');
    
    // load basic functions next so that everything after can use them
    require_once(LIB_PATH.DS.'functions.php');
    
    // load core objects
    require_once(LIB_PATH.DS.'session.php');
    require_once(LIB_PATH.DS.'database.php');
    
    // load database-related classes
    require_once(LIB_PATH.DS.'user.php');
    
    ?>
    My document root from phpinfo is /home/sites/mydomain.com/public_html/ - any help would be greatly appreciated.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what’s the string when you echo it?

    Comment

    • montaser

      #3
      u can user for apatchi for windows that code i think will be useful:
      Code:
      defined('DS') ? null : define ('DS' , DIRECTORY_SEPARATOR);
      
      if (DS=='/') 
        $absolute_path = dirname(__FILE__).'/'; 
      else 
        $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
        
      //echo $absolute_path;
      defined('SITE_ROOT') ? null : define ('SITE_ROOT' , $absolute_path);
      echo SITE_ROOT;
      Last edited by Niheel; Oct 27 '10, 10:17 PM.

      Comment

      • king enson

        #4
        beginner

        define('SITE_RO OT', DS.'Users'.DS.' kevin'.DS.'Site s'.DS.'photo_ga llery');

        this statement is relative to Kevin's computer site root. If you are using Windows, then siteroot is automatically pointing to your server. So I changed mine to this way. I did not need to specify photo_gallery bc i did not use it within www. I just copied the working files from chapter 6 into my server root file, in such a case, it would be C:wamp\www

        define('SITE_RO OT', DS.'wamp'.DS.'w ww');

        if you are also working on learning php/mysql, let me know, maybe we can help each other out.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          I usually use
          Code:
          define("BASE_DIR", realpath(dirname(__FILE__) . '/..') . "/");

          Comment

          Working...