Question about PHP includes

maw

Supreme [H]ardness
Joined
Sep 27, 2000
Messages
4,135
i'm in the process of converting a bunch of ASP pages over to PHP but have run into an annoying snag.

In teh old ASP pages, there are a bunch of include files, many of which resided in the root folder, so they can be accessed like this
Code:
<!--#include virtual="/filename.htm"-->

now this works great because i can use the same piece of code in any page, no matter how deep they reside in the directory tree. ASP knows to go to the root folder and then to the filename.

However, in PHP, this doesn't work, when i use:
Code:
<?php include("/filename.htm") ?>
i get an error message saying the include file cannot be found. This is a royal pain, since i now have to go to each file and put in the path like this:
Code:
<?php include("../filename.htm") ?> if it's in a sub-directory and
<?php include("../../filename.htm")?> if it's two levels down, etc...

is there some setting in PHP that i'm missing that would allow me to use includes like i did in ASP? BTW its on an IIS server.

Thanks
 
Perhaps the following?

Code:
<?php include($ROOT_DIR . "filename.htm") ?>

This way the code will be the same on any page you use it granted the $ROOT_DIR constant has been initialized.

--KK
 
PHP has include paths, which let you use include(filename.ext). Ideally, include files are then placed outside the document root but still inside PHP's include path. This can be configured in php.ini

Say you serve docs from /home/user/htdocs, you might create /home/user/includes for all included files.
(or, as a windows example, I have c:\webroot and c:\includes)

Also, you might want to check include_once(), require(), and require_once(), depending on context.
 
lomn75 said:
PHP has include paths, which let you use include(filename.ext). Ideally, include files are then placed outside the document root but still inside PHP's include path. This can be configured in php.ini

Say you serve docs from /home/user/htdocs, you might create /home/user/includes for all included files.
(or, as a windows example, I have c:\webroot and c:\includes)

Also, you might want to check include_once(), require(), and require_once(), depending on context.

OK, so it's just a matter of using a different technique. It's no prob moving all the include files to a different directory and accessinfg from there.

BTW i also tried using $_SERVER['DOCUMENT_ROOT'] . "/filename.htm" but i kept getting a "variable undefined" error or something like that, must be an IIS thing
 
maw said:
BTW i also tried using $_SERVER['DOCUMENT_ROOT'] . "/filename.htm" but i kept getting a "variable undefined" error or something like that, must be an IIS thing
Possibly so... I don't really know PHP on IIS well, but you can check by making a file consisting solely of
HTML:
<?php phpinfo(); ?>
and view it to see what variables are defined by default (bottom part of the page). See if one of those looks like what you'd expect for the doc root.
 
maw said:
BTW i also tried using $_SERVER['DOCUMENT_ROOT'] . "/filename.htm" but i kept getting a "variable undefined" error or something like that, must be an IIS thing
$_SERVER['DOCUMENT_ROOT'] would have been my suggestion. i assume you have php > 4. you could do as lomn75 suggested with phpinfo() or print_r($_SERVER) to see if it's in another array index because of IIS
 
thanks for the suggestions everyone :)

the most elegant solution was to modify the PHP include path as lomn75 suggested. now everything works great!!

oh, and the print_r($_SERVER) suggestion really brought back some interesting info.. very handy indeed.
 
lomn75 said:
PHP has include paths, which let you use include(filename.ext). Ideally, include files are then placed outside the document root but still inside PHP's include path. This can be configured in php.ini.
Did not know that. Very useful stuff.
 
Back
Top