How to include relative path URL

Discussion in 'PHP' started by kks_krishna, May 14, 2007.

  1. #1
    HI,

    how to include relative path url in PHP.
    I am getting the following error. If i am giving complete path its working fine:
    Warning: main(../templates/dlinks_header.php?user_name=): failed to open stream: No such file or directory in /home/content/k/k/s/kkskrishna/html/dlinks/links/links.php on line 47
     
    kks_krishna, May 14, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    What code are you using when you get this error?
     
    jestep, May 14, 2007 IP
  3. ccasselman

    ccasselman Peon

    Messages:
    412
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You should use require or require_once and then make sure you are going relative to the executing code not the first executed code.

    This is important when a file includes a file, includes a file and includes a file.

    Takes some tinkering on the server, but should be easy to figure out.
     
    ccasselman, May 14, 2007 IP
  4. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    links.php path is incorrect pls correct that one it will work.
     
    Subikar, May 14, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    First things first: you say 'relative path URL'. I assume you mean 'relative path' as you should not be including via a URL unless you know exactly why you're doing that (as opposed to including via the filesystem).

    I think you'll find that when A includes B and B includes C, C is included in the context of A, not B.

    For what it's worth, I'm going to copy and paste a couple of my posts from http://forum.joomla.org/index.php/topic,49344 to here (I'm Narcissus over there, in case you want to follow the whole thread which may be useful).

    Anyway...

    Note that that outcome is on my PHP installation which was 4.something at the time. The same result has been replicated on a number of machines that I've been able to test this on. It may be different on PHP5 but that doesn't matter for the solution I use.

    Instead of using relative paths by themselves, I use a combination of relative paths, __FILE__ and dirname.

    So, if I wanted to do:
    include( '../../a_file.php' );

    I would actually do:
    include( dirname( __FILE__ ) . '/../../a_file.php' );

    So, when I'm developing I can use relative paths but the beauty is that in reality, I'm using what are essentially 'dynamic absolute paths'. You don't have any problems with including the wrong files and you don't have to worry about changing the directory names when you move your code to another server.
     
    TwistMyArm, May 15, 2007 IP