SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I find document root?

    Hi,

    NOTE: wherever you see /localhost/ just imagine there is a "http:/" before it. This is because this forum doesn't allow you to enter a URL (even though strictly speaking it's not a URL you can access anyway) until you make 10 forum posts.

    I'm building a PHP based website on my local machine and because I have other sites under my localhost I have to put them into folders (e.g. /localhost/SITENAME/).

    The problem I'm having is with using absolute paths for my files (e.g. PHP include files, images, css, javascript).

    I want to use absolute paths so I can move my PHP web pages into sub folders (e.g. /localhost/SITENAME/PRODUCT-NAME/).

    The problem is that I have to manually type into each and every file path "/SITENAME/" at the start, because if I use just a forward slash / then it will try to find the file here: "/localhost/".

    I've tried using
    PHP Code:
    $_SERVER['DOCUMENT_ROOT'
    but that doesn't work as expected?

    I want to be able to use something like a PHP SERVER variable that can find the root of the website folder and so when I upload the site to the live server I don't have to change any code because the PHP variable will detect the new server root.

    Some of the $_SERVER variables show C:\ rather than /SITENAME/ and others such as $_SERVER['SCRIPT_FILENAME'] or $_SERVER['PHP_SELF'] show both the sub folder and the current file name (e.g. /localhost/SITENAME/PRODUCT-NAME/index.php) and it seems like it would be a server resource hog to have to call a function which explodes the string into an Array and then just use the relevant part of the URL that I need, but also that it might not work then as expected when putting the site on the live server and so again I have to change the original code which is something I want to avoid.

    Any help greatly appreciated as I'm sure there is a fairly simply solution.

    Kind regards,

    M.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    index.php:
    PHP Code:
    $root dirname__FILE__ );

    #Or for PHP 5.3:
    $root __DIR__
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi logic_earth, thanks for posting but this hasn't worked.

    If you take a look at the following example...

    PHP Code:
    echo "<a href='$root'>click here to go to home page</a>"
    Produces a link that tries to direct to the path...

    HTML Code:
    file:///C:/Inetpub/wwwroot/SITENAME
    ...which doesn't work when you click on the link.

    Ideally what I want it to do is to pick up /SITENAME rather than all the c:\ drive information.

    Any suggestions? As per my original post I don't want to have to run a function on every page that explodes the variable into an Array as that code would then break when the site is put on the live server (as it would no longer be in a folder called "SITENAME").

    Thanks.

    Kind regards,

    M.

  4. #4
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Integralist,

    You are confusing your terms. Document root is a term that corresponds to the physical location of the files on the server. It doesn't have anything to do with the URL that your site is accessed at. Both $_SERVER[ 'DOCUMENT_ROOT' ] and logic's method will help you find that.

    You are looking for the main folder that this site is accessed at. I can't think of a way for PHP to know that without changing code in a config file, if you're going to have the site at /SITENAME on localhost and just at the document root in production.

  5. #5
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by UFTimmy View Post
    I can't think of a way for PHP to know that without changing code in a config file

    I always thought this comes close, at least this is what I use:

    PHP Code:
    $filePath realpath(dirname(__FILE__));
    $rootPath realpath($_SERVER['DOCUMENT_ROOT']);
    $htmlPath str_replace($root''$filePath);

    echo 
    $htmlPath
    realpath makes sure that the paths exist and that the slashes are the same direction, independent of the OS. Then you just remove the documentroot from the file path...

    Am I missing something in this way of thinking?
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    I always thought this comes close, at least this is what I use:

    PHP Code:
    $filePath realpath(dirname(__FILE__));
    $rootPath realpath($_SERVER['DOCUMENT_ROOT']);
    $htmlPath str_replace($root''$filePath);

    echo 
    $htmlPath
    realpath makes sure that the paths exist and that the slashes are the same direction, independent of the OS. Then you just remove the documentroot from the file path...

    Am I missing something in this way of thinking?
    Yes, it'll fail for anything using mod_rewrite, apache aliases etc.

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Basically, the only to do it that I can think of that will cover all cases is a config file variable or something, or put a <base href=""> in your <head> section, making all links relative to there.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Basically, the only to do it that I can think of that will cover all cases is a config file variable or something, or put a <base href=""> in your <head> section, making all links relative to there.
    So, I have been struggling with the same thing. How would I use a config variable? Wouldn't I have to include the config file in all my other files? Which would defeat the purpose...
    --David Reagan
    DavidReagan.net

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As per what Jerrac said I did think to use a include file but it soon became apparent that it wouldn't work that well at all.

    Tomorrow I'll check out Fristi's option as I'm not using Apache but a Windows server so the issue that was raised by Stormrider should hopefully not affect my use case. If it works I'll post my code here in case it helps any one else.

    Thanks again for everyone's post, I'll let you know tomorrow how far I get or whether it's back to the drawing board.

    Kind regards,

    M.

  10. #10
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jerrac View Post
    So, I have been struggling with the same thing. How would I use a config variable? Wouldn't I have to include the config file in all my other files? Which would defeat the purpose...
    Yes. Why would that defeat the purpose?

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because I would have to include the config file with the full path to get the variable I wanted, when the purpose of that variable is to avoid have to use the full path.

    Unless I am missing something obvious...
    --David Reagan
    DavidReagan.net

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, you can include the relative path to the config file:
    PHP Code:
    include dirname(__FILE__) . "/../config.php"
    ..for example, and in any case, you are getting path mixed up with URL, like the original poster did. We can find the path easily, it's the URL we want in the config file.

  13. #13
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just so you know these solutions didn't work out for me so it's back to the drawing board I'm afraid.

    If I find out anything more then I'll let you know.

    M.

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What i normally do is, just have a configuration file and define the constants with your PATHs/URLs like this:
    PHP Code:
    if($_SERVER['HTTP_HOST'] == "localhost"){// For local
        
    define('SITE_URL''http://' $_SERVER['HTTP_HOST'] . '/mysitename');
        
    define('SITEPATH'$_SERVER['DOCUMENT_ROOT'] . '/mysitename');
    }
    else{ 
    // For Web
        
    define('SITE_URL'"http://" $_SERVER['HTTP_HOST']);
        
    define('SITEPATH'$_SERVER['DOCUMENT_ROOT']);

    Include the config file at the top of the page and you can use the SITE_URL and SITE_PATH wherever you needed.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm just being blind but why can't you just write the base path to somewhere readily available in your script (a config file, or whatever) and use that value wherever it is needed? Depending on your file structure that might be an easy option.
    Salathe
    Software Developer and PHP Manual Author.

  16. #16
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    here is what I do, place an empty file called root.php or whatever extension you want in the root.

    then
    PHP Code:
    if (file_exists("./root.php")){
    $rpath '.';
    }
    elseif (
    file_exists("../root.php")) {
    $rpath '..';
    }
    elseif (
    file_exists("../../root.php")) {
    $rpath '../..';
    }
    // add more elseif to get your paths 
    Then in the code you can call images/links like
    PHP Code:
    <a href="<? echo $rpath ?>/link">
    <img src="<? echo $rpath ?>/b-img/b-news.gif" width="200" height="30" border="0" class="navbutton">
    </a>
    This is the only method that is bulletproof for me as the menu can be included at any depth in directories and will always work.
    What I lack in acuracy I make up for in misteaks

  17. #17
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Integralist View Post
    Hi logic_earth, thanks for posting but this hasn't worked.

    If you take a look at the following example...

    PHP Code:
    echo "<a href='$root'>click here to go to home page</a>"
    Produces a link that tries to direct to the path...

    HTML Code:
    file:///C:/Inetpub/wwwroot/SITENAME
    ...which doesn't work when you click on the link.

    Ideally what I want it to do is to pick up /SITENAME rather than all the c:\ drive information.
    file:///C:/Inetpub/wwwroot/SITENAME[/HTML] is a IIS windows server root address.

    Are you using IIS as your dev server or Apache, if you are using IIS this could explain why none of the fixes offered by others here are working.

    The advice most here are offering you is based on the assumption that you are running apache, not IIS

  18. #18
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WavyDavy View Post
    ...The advice most here are offering you is based on the assumption that you are running apache, not IIS
    Ummm they would work with IIS just fine. Nothing posted here is server-software-dependent.
    PHP on IIS or Apache is not some vastly different language.

    ----------------------------------------------------------

    If one was following an MVC approach to a web application, or any approach that would funnel every request to a single file (mostly index.php). Then all your problems dealing with paths would come to an end. Second during development building every site or application as it were real is key. By that I mean giving it a real domain and isolated path. (No localhost/site-or-app-name)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •