1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Setting root directory in php

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

  1. macrumors 6502a


    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:


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


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

    <?php require('file.php'); ?>
    require('file2.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


    you could use relative pathing...
    <?php require('../../file.php'); ?> 
  3. macrumors 601



    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:
    ...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. macrumors 6502a


    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


    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 require('./file.php'); ?> 
  6. macrumors 601


    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. macrumors 6502a


    I seemed to get some sucess if I added

    Before the include tags. It does, however, still leave me with the problem that the style.css isn't being read.
  8. macrumors regular

    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. macrumors 6502a


    Fixed the problem with the reading of the style.css file. Basic html rules that I just happen to forget.


  10. macrumors 601


    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


    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


    $BaseDir = '../../../'; 
    in the parent file...

    Then in the include file

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


    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!

  14. macrumors 65816


    Just hope you never change servers :)
  15. macrumors 6502a


    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

    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


    You're right, I read it wrong :)
    I've never tried that via htaccess though

Share This Page