SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Include path ??

  1. #1
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I'm trying to use include () function in my php script. But I just don't know what is the right way or professional way to do this.

    Should I use environmental variables like $DOCUMENT_ROOT, $SERVER_NAME, $HTTP_HOST ?

    How to use this environmental variable with the include() function ?

    Is this alright ?

    include("$DOCUMENT_ROOT/myinclude/filename.php");
    Ngu I.P.
    Web Developer

  2. #2
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I understand your question entirely, but I just point to the file locally, like include("file.inc"), or include("../file.inc"), or include("directory/file.inc")
    Ed Rands

  3. #3
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...

    I tried what you did... but it give me error when I try to include the file from a external directory.

    Anyway, I tried that

    include("$DOCUMENT_ROOT/myinclude/filename.php");
    Ngu I.P.
    Web Developer

  4. #4
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The path for an include in PHP is just like the source for an image. If the file to include is in the same directory as the calling file, it will be include("file.inc").

    If the file to include is one directory higher, as in...

    yourwebsite/file.inc
    yourwebsite/mainfiles/somefile.php

    then the include would look like include("../file.inc"). The "../" tells PHP to look one directory or level higher than the file that wants the include.

    Or, if the file to include is in a directory one level lower, as in

    yourwebsite/somefile.php
    yourwebsite/includes/file.inc

    you would want include("includes/file.inc").

    Hope you're getting all this. If I haven't confused you enough, here's a good one. If the PHP file and the file to include are each in seperate directories, like this...

    yourwebsite/phpfiles/somefile.php
    yourwebsite/includes/file.inc

    then you would use include("../includes/file.inc"), which tells PHP to back up one level, then to look in the specified directory.

    Anyway, I hope in something I said you can find something useful.
    Ed Rands

  5. #5
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried to use relative path like

    include("../phpdir/file1.php"); it give me errors.

    My setup is like this:-

    /home/user/username/mydomain/phpdir/file1.php

    then in this file1.php, I have this line

    <img src="images/pic2.jpg" width="720" height="80" border="0">

    This file1.php is working file if I include this in
    /home/user/username/mydomain/phpdir/file2.php

    with this line
    include("file1.php");

    but if I want to include this file1.php in
    /home/user/username/mydomain/dir1/index.php

    with this line
    include("../phpdir/file1.php");

    it give me error regarding the include path and also the pic2.jpg is not displaying.

    now, when I changed the inlcude to this
    include("$DOCUMENT_ROOT/phpdir/file1.php");

    and
    <img src="http://<? $SERVER_NAME/phpdir/images/pic2.jpg";?>" width="720" height="80" border="0">

    It's working fine without error. So, do you know why is this happenning ?
    Ngu I.P.
    Web Developer

  6. #6
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't a clue as to why your troubles are happening. Sounds very strange to me. I'll have to leave it to someone with more experience to answer this one.
    Ed Rands

  7. #7
    SitePoint Enthusiast nguip's Avatar
    Join Date
    Apr 2001
    Location
    Malaysia
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It look strange to me either... Anyway I have solved my problem with the above method..

    Just curious why this is happening.
    Ngu I.P.
    Web Developer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •