Setting root directory in php

Discussion in 'Web Design and Development' started by cooknwitha, Feb 19, 2006.

  1. macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #1
    Back again with another simple php question that I just can't find an answer to!

    In my main page, I need to call up a bunch of separate php files. Most are located in the root directory so:

    /public_html/file.php
    /public_html/file2.php
    /public_html/file3.php

    The file that is calling up these files is in a directory a few down like:

    /public_html/dir1/dir2/content.php

    During my test phases, the content file was in the root directory and these commands worked

    PHP:
    <?php require('file.php'); ?>
    <?php 
    require('file2.php'); ?>
    <?php 
    require('file3.php'); ?>
    But now that it's been moved down a few directories, this is no longer working. So, obviously it doesn't work like html.... I figured that out for myself.

    Now, there's obviously an easy way to get around this but I can't find it written in "stupid people language" for me to understand.

    Can anyone help me?
     
  2. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #2
    you could use relative pathing...
    PHP:
    <?php require('../../file.php'); ?> 
     
  3. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #3
    or....

    i don't use require, i use the include statement, so i'm not certain if the following will work for require.

    i put all my "include" files into /public_html/include, then include them from sub directories of /public_html like this:
    PHP:
    <? 
    include 
    "header.inc.php";
    include 
    "style.css";
    ?>
    ...and it finds them.

    fwiw, i'm not really a php programmer, so i don't know if i'm using best practices here. perhaps someone with more experience can chime in on whether what i'm doing is accepted practice.

    but it works for me.

    edit: if you do use the include directory, i recommend you throw an index file in there, as well, so no one can browse your files.
     
  4. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #4
    I tried that but it seems to disregard my style.css file which, I have located in header.php which is located in the public_html directory. The style.css is also in the public_html directory.
     
  5. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #5
    First, off require and include do the exact same thing, although in previous php ver they were different.

    try this if it is a unix server:

    PHP:
    <?php require('./file.php'); ?> 
     
  6. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #6
    good to know, thanks. any idea if my use of the include directory is a common practice? coming from a c/c++ background, it made sense to me :)
     
  7. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #7
    I seemed to get some sucess if I added

    PHP:
     ini_set("include_path""../../")
    Before the include tags. It does, however, still leave me with the problem that the style.css isn't being read.
     
  8. macrumors regular

    Joined:
    Sep 4, 2004
    #8
    Yeah, it is.

    Also, at least in PHP4, there is a slight difference between include (as well as include_once) and require (as well as require_once) in the handling of errors. If you try and include a file and it fails, PHP will generate an error and, depending on your set reporting level, may print it. If you try and require a file and it fails, PHP stops processing the page.
     
  9. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #9
    Fixed the problem with the reading of the style.css file. Basic html rules that I just happen to forget.

    Whoops!

    :eek:
     
  10. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #10
    right, i just remembered why my php code finds them. in my /public_html/.htaccess file, i've got this line:

    php_value include_path ".:/home/username/public_html/include/"
     
  11. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #11
    hmm, I dunno... I didn't learn that they weren't different until php4. I just use include now. My guess, that most people who learned after php4 and were "C guys" use include as well.
     
  12. macrumors 65816

    ChicoWeb

    Joined:
    Aug 16, 2004
    Location:
    California
    #12
    Code:
    <?php		  
    $BaseDir = '../../../'; 
    include("header.php"); 
    ?>	
    in the parent file...

    Then in the include file

    Code:
     <a href="<? print $BaseDir; ?>
    You'll have to do that with your images/bgs or anything that relates to a path..
     
  13. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #13
    This is exactly what I wanted and it works. By doing that (minus your /include/) I can now operate it like html which is what I want. It might not be the correct way but it works fine and will make life easier for me!

    Thanks
     
  14. macrumors 65816

    ChicoWeb

    Joined:
    Aug 16, 2004
    Location:
    California
    #14
    Just hope you never change servers :)
     
  15. thread starter macrumors 6502a

    cooknwitha

    Joined:
    May 5, 2005
    Location:
    London
    #15
    hmmmm..... but if I were to change servers, provided I don't muck up the directory structure, wouldn't I just need to change the info in the .htaccess file?
     
  16. macrumors regular

    Joined:
    Apr 11, 2003
    #16
    you might want to try '/yourFile.php'. the leading / represents the root so if your calling files move around but the includes stay at the root, the calling files will always go directly to them. the whole ../ stuff can get confusing very easily if you're trying to access the same includes from different directory levels.
     
  17. macrumors 65816

    ChicoWeb

    Joined:
    Aug 16, 2004
    Location:
    California
    #17
    You're right, I read it wrong :)
    I've never tried that via htaccess though
     

Share This Page