include() from another directory within web root?

Discussion in 'PHP' started by chris062689, Jul 20, 2008.

  1. #1
    Here's my file tree right now..

    /htdocs
    /htdocs/poemgenerator
    /htdocs/poemgenerator/index.php
    /htdocs/poemgenerator/poems.db
    /htdocs/poemgenrator/include/db.inc.php

    When I'm editing index.php, I include this line...
    require_once('/inc/db.inc.php');

    But it gives me this error:

    Warning: require_once(/inc/db.inc.php) [function.require-once]: failed to open stream: No such file or directory in C:\Documents and Settings\Chris\My Documents\xampp\htdocs\poem_gen\index.php on line 2

    Fatal error: require_once() [function.require]: Failed opening required '/inc/db.inc.php' (include_path='.;C:\Documents and Settings\Chris\My Documents\xampp\php\pear\') in C:\Documents and Settings\Chris\My Documents\xampp\htdocs\poem_gen\index.php on line 2


    Why? :(
     
    chris062689, Jul 20, 2008 IP
  2. domainsurfer

    domainsurfer Well-Known Member

    Messages:
    922
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    include path starting with / means start from the root.

    You can include relative paths using :
    the dot (.) indicates path from the current directory.
     
    domainsurfer, Jul 20, 2008 IP
  3. chris062689

    chris062689 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is there a function that outputs all files in a directory?
    I want to see what exactly '/' is.

    Or do you mean '/' = 'C:\'
     
    chris062689, Jul 20, 2008 IP
  4. domainsurfer

    domainsurfer Well-Known Member

    Messages:
    922
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    / doesnt mean anything.
    Its just the "Windows" way of representing levels of directories.
    But it doesnt work in web hosting , webservers.
    You have to specify levels using /

    If your script is located in the same directory as db.inc.php, then do this
    If your script is located in inc directory,

    .. means up one level
    . means current directory

    cant make it any more simpler than this.
     
    domainsurfer, Jul 20, 2008 IP
    nicangeli likes this.
  5. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can alternatively define some path constants. In the first bits of code that get executed for your application, do something like this:

    define ('DIR_BASE', dirname(__FILE__));   // This will return the directory from ROOT to THIS file
    define ('DIR_INCLUDE', DIR_BASE . DIRECTORY_SEPERATOR . 'include');
    PHP:
    Then in other portions of your script, you can include a file like so:

    require_once DIR_INCLUDE . DIRECTORY_SEPERATOR . 'somefile.php');
    PHP:
    You can choose to include a trailing slash or not in your constant based on your preference.
     
    ahowell, Jul 21, 2008 IP