Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cooknwitha

macrumors 6502a
Original poster
May 5, 2005
562
0
London
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?
 
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.
 
zimv20 said:
you could use relative pathing...
PHP:
<?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.
 
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'); ?>
 
superbovine said:
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 :)
 
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.
 
zimv20 said:
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.
 
Fixed the problem with the reading of the style.css file. Basic html rules that I just happen to forget.

Whoops!

:eek:
 
zimv20 said:
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/"
 
zimv20 said:
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.
 
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..
 
zimv20 said:
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
 
cooknwitha said:
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 :)
 
ChicoWeb said:
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?
 
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.
 
cooknwitha said:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.