PDA

View Full Version : Setting root directory in php




cooknwitha
Feb 19, 2006, 10:17 PM
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 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?



zimv20
Feb 19, 2006, 11:30 PM
you could use relative pathing...
<?php require('../../file.php'); ?>

zimv20
Feb 19, 2006, 11:39 PM
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:

<?
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.

cooknwitha
Feb 19, 2006, 11:39 PM
you could use relative pathing...
<?php require('../../file.php'); ?>

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.

superbovine
Feb 19, 2006, 11:57 PM
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'); ?>

zimv20
Feb 20, 2006, 12:14 AM
require and include do the exact same thing
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 :-)

cooknwitha
Feb 20, 2006, 12:20 AM
I seemed to get some sucess if I added

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.

jestershinra
Feb 20, 2006, 12:36 AM
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 :-)

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.

cooknwitha
Feb 20, 2006, 01:14 AM
Fixed the problem with the reading of the style.css file. Basic html rules that I just happen to forget.

Whoops!

:o

zimv20
Feb 21, 2006, 12:59 AM
i put all my "include" files into /public_html/include, then include them from sub directories of /public_html [...] and it finds them.

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/"

superbovine
Feb 21, 2006, 01:02 AM
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 :-)

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.

ChicoWeb
Feb 21, 2006, 01:14 AM
<?php
$BaseDir = '../../../';
include("header.php");
?>

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..

cooknwitha
Feb 21, 2006, 02:23 AM
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/"

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

ChicoWeb
Feb 21, 2006, 10:48 AM
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

Just hope you never change servers :)

cooknwitha
Feb 21, 2006, 05:16 PM
Just hope you never change servers :)

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?

Thom_Edwards
Feb 21, 2006, 06:13 PM
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.

ChicoWeb
Feb 21, 2006, 10:21 PM
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?

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