include "../index.php" will catch the domain.com/index.php?

Discussion in 'PHP' started by Davidf25sc, Oct 19, 2006.

  1. #1
    Hey i have a little problem, i have a site like this
    http://www.domain.com/sub1/sub2/what2.php
    inside what2.php i want to include the file located here :
    http://www.domain.com/what2.php

    How should i do it?
    include "../what2.php"? <- this will only work if its inside sub1
    include "http://www.domain.com/what2.php"? <- what if i move the site to another domain?

    Thanks for your help.
    David.
     
    Davidf25sc, Oct 19, 2006 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    use full path:

    /home/username/domain.com/publichtml/what2.php

    Peace,
     
    Barti1987, Oct 19, 2006 IP
  3. Davidf25sc

    Davidf25sc Active Member

    Messages:
    165
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    isnt there a more "generic" way? something that will work with any domain and username? like ../
     
    Davidf25sc, Oct 19, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Firstly, you could always use '../../what2.php'.

    Secondly, including 'http://www...' never works as you would expect. It doesn't just open the file like a normal include does, as it requests the file via HTTP (and so you end up including the parsed output).

    Thirdly, using the full path is the best way to go as including files in one directory that include files in another directory doesn't work as you'd expect, either. However, you can dynamically determine the full path, too, by using dirname(__FILE__).

    If I want to include '../what2.php', I use:
    include dirname(__FILE__) . '../what2.php';

    instead.

    In your case, you probably want:
    include dirname(__FILE__) . '../../what2.php';

    Hope that all helps.
     
    TwistMyArm, Oct 20, 2006 IP