Forum Moderators: coopster

Message Too Old, No Replies

PHP: includes and directories

How do I include /dir/files that include their own files?

         

freshmold

8:12 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



I'm fairly new to PHP and I'm trying to figure out a problem. A site I'm working on will have a forum and gallery and other things. To keep things organized, each prog has it's own directory (like ./forum/ and ./gallery/).

What I want to do is have those things load into ./home/index.php - like a template. The problem is the files in those directories require/include their own files and need their own sub-directories to function properly.

So, the question is: How do I arrange and code my site so that everything loads into the index.php template without problems?

Thanks in advance.

- fresh

jatar_k

9:28 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld freshmold (funny :))

Remember that when including files the paths must work from the file which is including them. If you use root relative includes you should be fine. Then no matter where on the site the template is included you paths are correct.

you could also use $_SERVER['DOCUMENT_ROOT'] [php.net] with your includes, if needed, to reference the root of the site.

'DOCUMENT_ROOT'
The document root directory under which the current script is executing, as defined in the server's configuration file.

russgri

12:08 am on Apr 1, 2003 (gmt 0)

10+ Year Member



I wonder...if he did not need more info...I know I do..
Is this a super global that does not need to be defined.
and..
//just what would be the syntax if
server root shown on a my site would be if my root was:

/home/username/mysite.com-www

$_SERVER['DOCUMENT_ROOT']

//and my include folder was "includes"
<?php

include($includesDir . 'pageFooter.inc.php');
?>
How would I weave this all together?

freshmold

12:16 am on Apr 1, 2003 (gmt 0)

10+ Year Member



K, I'm trying out those suggestions now.

While we wait, I'm thinking of another method. Is it possible to create an htaccess that automatically adds a header and footer to every file in certain directories?

Thanks again.

andreasfriedrich

12:46 am on Apr 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your .htaccess file you can tell PHP [php.net] to prepend and append a certain file to the main file being called.

See the documentation for the auto_prepend_file [php.net] and auto_append_file [php.net] directive.

Andreas

andreasfriedrich

12:48 am on Apr 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SERVER is a superglobal.


<?PHP [php.net]
include($_SERVER['DOCUMENT_ROOT' . $includesDir . 'pageFooter.inc.PHP [php.net]');
?>

will include pageFooter.inc.PHP [php.net] using an absolute path.

Andreas

russgri

2:45 am on Apr 1, 2003 (gmt 0)

10+ Year Member



Andreas wrote
<code><?PHP include($_SERVER['DOCUMENT_ROOT' . $includesDir . 'pageFooter.inc.PHP'); ?></code>

will include pageFooter.inc.PHP using an absolute path.
-------
//Question.
$IncludesDir is has to be defined in a file called paths.inc with the following line...

$includesDir = 'includes/'; // includes directory

and if so is the above line correct including slashes, punctuation etc?

?is paths.inc or indeed config.php global control pages or would any other name suffice?

or...
If a page contains:
$includesDir = 'includes/';
or a similar variable...does that make it the control mechanism regardless of the name?

jatar_k

3:09 am on Apr 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



file called paths.inc

but that file would still have to be included. ;)

$includesDir = 'includes/'; // includes directory

but

The include() [php.net] statement includes and evaluates the specified file.

just stating that because I don't quite understand the direction of the thought. Better might be

$includesDir = $_SERVER['DOCUMENT_ROOT'] . 'includes/';

does that make it the control mechanism regardless of the name?

It can. If all of your included files are in that directory you could use that for including files. Again, I don't totally understand what you are asking specifically.

<sidenote>Have you ever tried typing the path to a file with a .inc extension into your browser? It shows the code. My personal preference is not to show anyone what I am doing on the server side. .inc.php or just .php would be preferable to keep server side code on the server side. (personal paranoia ;))

russgri

2:35 am on Apr 3, 2003 (gmt 0)

10+ Year Member



Andreas wrote:
<?PHP
include($_SERVER['DOCUMENT_ROOT' . $includesDir . 'pageFooter.inc.PHP');
?>

will include pageFooter.inc.PHP using an absolute path.

This is a simple and elegant post and it nailed the question.

However: ...correct me if I am wrong...

I still have to define the name of the include folder or type the name of the folder in each include

So... I still have to create a file names..
paths.inc and place it in the root folder.
and in it jatar offered this solution:

$includesDir = $_SERVER['DOCUMENT_ROOT'] . 'includes/';
----
and in index.php I will need to include it thus:

<?PHP
include($_SERVER['DOCUMENT_ROOT' . $includesDir . 'paths.inc.');
?>

Please comment.

jatar_k

5:26 am on Apr 3, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nope,

like this

include $includesDir . 'paths.inc';

we already built $DOCUMENT_ROOT into the var.