base directory path

Discussion in 'Software' started by LadyYepperz, Nov 16, 2008.

  1. LadyYepperz

    LadyYepperz Private E-2

    ok.. im working in php and in a script i need to include my base directory path.
    where da heck do i find it?? im trying to use the php
    include() function on my html pages


    PHP:
    <?php
    //ive tried... 

    echo  getcwd(); 

    //and...
     
    echo  phpinfo(); 
     
    //and...
     
    echo ($_SERVER['DOCUMENT_ROOT']); 
     
    //and...
     
    echo $_SERVER["SCRIPT_FILENAME"];
    ?>
    ok... tho ive tried all of those to find my base directory.... with no success... ive decided to try yet again.

    then i thought, "maybe the problem is in my script"
    so, here are those...

    definition.inc.php

    PHP:
    <?php 
             define
    ("BASE_DIRECTORY""/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/"); 
             
    ini_set("include_path"BASE_DIRECTORY."includes/"); 
    ?> 
    body.html
    PHP:
    <?php 
            
    include("definition.inc.php"); 
            include(
    "header.inc.php"); 
    ?>
    can you tell me... is the problem in my scripts or still in the path??

    p.s. that orange in the php tags is all bad! Yikes! MG needs to rethink the comment color.
     
  2. w3d

    w3d Private E-2

    $_SERVER['DOCUMENT_ROOT'] would normally be your 'base directory' or web root. You would, however, need to append a slash to this in order to build a complete path:
    PHP:
    include($_SERVER['DOCUMENT_ROOT'].'/includes/myinclude.php');
    HOWEVER, are you on IIS? In which case, DOCUMENT_ROOT is not available! But it can be worked out by messing around with some of the other superglobals... SCRIPT_NAME and SCRIPT_FILENAME etc. This may depend on what your server is returning.

    What does phpinfo() show?
     
  3. LadyYepperz

    LadyYepperz Private E-2

    $_SERVER['DOCUMENT_ROOT'] returns /www/

    Where do i find that?

    SCRIPT_NAME returns /info.php
    SCRIPT_FILENAME returns /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/info.php
    include_path returns .:/usr/share/php (dont know if this is important or not)
    SCRIPT_URI returns http://ladyyepperz.oxyhost.com/info.php

    is there anything else you think might be important?
     
  4. PC-XT

    PC-XT Master Sergeant

    You might try these, one of them may work?
    Code:
    /l/a/d/ladyyepperz/htdocs/
    /
    (That last one is just a slash by itself.)
     
  5. w3d

    w3d Private E-2

    IIS (Internet Information Server) is Microsoft's webserver more commonly found on Windows servers. As opposed to the open source Apache webserver more commonly found on Linux servers.

    The fact that DOCUMENT_ROOT is set at all does imply you are on Apache (and the server path looks Linux-like). However, the DOCUMENT_ROOT that is being returned is not what you are expecting! (This is probably just down to how the server is configured and if you are on a shared host (or part of a larger organisation) there is probably little you can do about it!)

    Presumably the DOCUMENT_ROOT you are after is...?
    You will need to somehow set this at the top of each script that includes files. But you want to avoid hard-coding this path everywhere in case you move servers at a later date?

    You could do something like:

    Create a config.php file in your webroot (document root / base directory - whatever you want to call it)
    PHP:
    <?php
    // config.php
    // Set whatever is the base directory
    // This replaces SCRIPT_NAME in SCRIPT_FILENAME with an empty string (two single quotes)
    // to hopefully return just the base directory.
    // (You could also join this onto the include_path if you wanted)
    define('BASE_DIRECTORY',str_replace($_SERVER['SCRIPT_NAME'], ''$_SERVER['SCRIPT_FILENAME']));
    ?>
    Use the auto_prepend_file directive in .htaccess to append this file to the beginning of every file:
    Code:
    # .htaccess
    # Needs to be hard coded unfortunately - but only once
    auto_prepend_file /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/config.php
    
    Although the $_SERVER[] array is reasonably consistent between similar servers... it is not guranteed to be the same!

    Hope that helps.
     
    Last edited: Nov 17, 2008
  6. LadyYepperz

    LadyYepperz Private E-2

    @W3D

    thank you for that information as i am still a student of PHP and server side scripting... I am indeed on an Apache server... I will take what you've said into consideration and implement your suggestions but....

    after renaming my body.html to body.php.. i received the following errors...
    maybe you can help me understand what they mean...


    Warning: include(definition.inc.php) [function.include]: failed to open stream: No such file or directory in /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/body.php on line 2

    Warning: include() [function.include]: Failed opening 'definition.inc.php' for inclusion (include_path='.:/usr/share/php') in /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/body.php on line 2

    what is it saying??
    looks to me like i need to use
    include_path='.:/usr/share/php' as my base directory path (document root) or as my include path... but im really not sure... so before i do anything ill wait to see what you think.
     
  7. LadyYepperz

    LadyYepperz Private E-2

    i used the following script

    echo get_include_path();

    and it returned...

    .:/usr/share/php/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/includes/

    is this what i should be using?... im so freakin confused! if this is my include path... it still doesnt tell me what my document root is... :confused
     
  8. w3d

    w3d Private E-2

    The include_path is the set of paths (yes, there can be more than 1, separated by ':' on Apache/Linux) that PHP looks in when you try to include a file. The path/file you specify in the include() needs to be relative to one of these paths (unless you specify a root-relative path - which is a bit impractical). Usually the first path specified in the include_path is the '.' (period/dot) indicating the current working directory (CWD). Many people probably just include files relative to the CWD, which is fine for a few casual includes, but in a more involved webapp with reusable components this could become an unnecessary headache to maintain. So, you can set the include_path to your DOCUMENT_ROOT (or specifically to your include folder as you are doing in your OP).

    (Sorry, I didn't spot the .html extension earlier) As you probably realise, without the .php extension your file is not (normally) parsed by PHP and consequently your <?php ?> blocks do not get executed.

    Those warnings are just saying it can't find your includes. The path is wrong.

    I think in your case, it's a simple matter of chicken and egg... you are trying to include "definition.inc.php" assuming that you have already set the include_path - which you do in "definition.inc.php"! There lies your problem.

    Since your body.php file is already in your webroot and your includes folder is a sub folder off your webroot then you could simply include your files relative to the CWD (and not bother setting your include_path, since the include_path already includes the CWD) as mentioned above:
    PHP:
    <?php
    // body.php
            
    include("includes/definition.inc.php"); 
            include(
    "includes/header.inc.php"); 
    ?>
    To get it to work the way you were wanting you would need to insert the contents of definition.inc.php at the top of body.php - which is a bit impractical if you are hard coding the include_path and not using the $_SERVER[] variables. OR you could use the method I mentioned below to calculate your document root using str_replace() and $_SERVER['SCRIPT_FILENAME'] etc. OR use the auto_prepend_file directive in your .htaccess file to automatically prepend your definition.inc.php to every file (ie. body.php) - sorry, I missed the php_value instruction in my post below!
    Code:
    # .htaccess
    # Needs to be hard coded unfortunately - but only once
    php_value auto_prepend_file /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/includes/definition.inc.php
    and keep your body.php file as it is.

    The above (htacces) code would go in the ".htaccess" file in your webroot (it may not already exist) and would effect the webroot and every sub folder. Do you know about .htaccess files?
     
  9. LadyYepperz

    LadyYepperz Private E-2

    im very, VERY new to php and ALL server side scripting...
    in fact im learning as i build this site
    so... no... i dont know about .htaccess files.
    i am totally confused and lost... :cry
    i want to thank you for taking the time to help me..
    and i ask that you please be patient with me.
     
  10. LadyYepperz

    LadyYepperz Private E-2

  11. LadyYepperz

    LadyYepperz Private E-2

     
  12. PC-XT

    PC-XT Master Sergeant

    try
    Code:
    <?php
    // body.php
            include("./definition.inc.php"); 
            include("includes/header.inc.php"); 
    ?>
    (add "./" to the first include)
     
  13. LadyYepperz

    LadyYepperz Private E-2

    ty!! that worked!! no more problem!:-D
     
  14. w3d

    w3d Private E-2

    May be to both, since both includes look like they should be relative the current directory? (a single dot in the path indicates the current working directory - CWD)

    But... what really is the current include_path?

    You stated earlier that:
    Which includes the CWD (the dot before the colon at the start of the include path), so having to specify './' in your include() shouldn't be necessary...?

    However, in your original code snippet:
    ...would ordinarily replace the current include_path completely with what you specify (removing the CWD ".:" part).

    In order to add to the current include path (rather than replace it entirely) then it is more usual to do something like:
    Code:
    ini_set("include_path", BASE_DIRECTORY . "includes/" . PATH_SEPARATOR . ini_get("include_path"));
    Which adds your choice of include path to the beginning of the current include path.

    (I hope all this isn't getting too confusing!?)
     
  15. w3d

    w3d Private E-2

    Glad you got it sorted. (I didn't see your post earlier!)

    I'm still curious as to what your include_path is being reported as {echo get_include_path();}, before and after including your definition.inc.php ? Thanks. :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds