default root folder path

Discussion in 'PHP' started by cn45896, May 13, 2010.

  1. #1
    I have a site that has a variable that holds the path of the root level for each page. It sometimes gives google errors and I want to update it and know there is some kind of better way to reference in PHP. Any tips would be great. currently it looks like this:

    img src='<php echo $basePath; ?>images/something.jpg
     
    cn45896, May 13, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You can do some funky stuff when you mess about with code like this
    $folder = str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
    
    define('ABSPATH', dirname(dirname(__FILE__)).'/');
    PHP:
    In this example plugin_basename is a wordpress function but the rest is good.

    See also: http://php.net/manual/en/language.constants.predefined.php
     
    sarahk, May 13, 2010 IP
  3. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Make a PHP file say, path.php with the following Codes:
    <?php
    $abspath = dirname(__FILE__).'/';
    echo $abspath;
    ?>
    PHP:
    Now place the file on the root of your site and run it through your browser. Now copy the output! Say it is

    /home/yoursite/public_html/
    Code (markup):
    Copy it and place the following code, in the file where you have previously stored the variable for the absolute path:
    define(ABSPATH, '/home/yoursite/public_html/');
    PHP:
    Now all you need to do is use the constant string instead of the variable string. Say
    $my_img = '<img src="'.ABSPATH.'images/img.jpg" />';
    echo $my_img;
    PHP:
     
    swashata, May 14, 2010 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Your browser won't be able to handle absolute paths, use them for server side programming and the web paths for the html and client side code.
     
    sarahk, May 14, 2010 IP
  5. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Oh yaa that's true! Mann I did replied as a dumbo :|
     
    swashata, May 14, 2010 IP
  6. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #6
    The following code should work to get the Root directory [Browser type] of any page!
    <?php
    $site_url = 'http://'.$_SERVER['SERVER_NAME']; //The URL of the main site... Say http://forum.digitalpoint.com
    $cur_dir = $_SERVER['REQUEST_URI']; //The other part of URL of the current page... say /showthread.php
    $root_dir = dirname(dirname($cur_dir)); //Go one level up the current directory
    //Basically the first dirname gives the directory of the current file/page and the second one moves up one level
    $root_url = $site_url.$root_dir.'/'; //Concatenate the strings to make the browser URL of the root directory
    define(ABSPATH, $root_url);
    $my_img = '<img src="'.ABSPATH.'images/img.jpg" />';
    echo $my_img;
    ?>
    PHP:
    Sorry for the previous wrong post! Actually I did wrote a tutorial on it before. You may want to see this to clarify your doubts!

    I hope I haven mistaken anything this time :)
     
    swashata, May 14, 2010 IP
    sarahk likes this.
  7. assertivemagazine.com

    assertivemagazine.com Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    thank you!
     
    assertivemagazine.com, May 14, 2010 IP