include("/main.php") impossible

Discussion in 'PHP' started by winterheat, Jun 18, 2007.

  1. #1
    if you are just a users among the 100s of users on a hosting machines,
    then if you do a include("/main.php"); it won't include the file
    but will say

    Failed opening required '/main.php' (include_path='.:/usr/local/nf/
    lib/php')

    it seems ok to include("../main.php"); except the file will not work
    if moved from www.abc.com/movies to a new location of
    www.abc.com/movies/little-mermaid is there a better solution for
    this?
     
    winterheat, Jun 18, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You should store a variable in your app with the absolute path. You can use $_SERVER['DOCUMENT_ROOT'] and $_SERVER['PHP_SELF'] to find it automatically (so you don't have to change a config file every time you move server or put the app in a new directory).
     
    krt, Jun 18, 2007 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ^^ What he said. Alternatively, I use something similar to the form:
    include( basedir(__FILE__) . '../main.php' );

    which determines the directory that the current file is in and then uses the 'relative' portion of the path to create an absolute path.
     
    TwistMyArm, Jun 19, 2007 IP