Help - Search - Members - Calendar
Full Version: php include from a different directory?
HTMLHelp Forums > Programming > Server-side Scripting
nWo Sting
I know the basic php include code, <?php include("header.php"); ?>

But that only works if the include file is in the same directory. I want to make several directories, and it will be a hassle to put these include files in each directory. I was wondering what the exact php include code is for using includes from a different directory within the same website. I read somewhere that it was something like <?php include("_header.php"); ?>

.......but I did not understand if I was supposed to add something before the underscore. I tried adding a slash mark / before the header and correct directory name, and also tried inputting my entire web site url plus directory path that the include codes are in, but those did not work. Can I still use the same original include code but add something in front of header? Thanks
Christian J
If the included file is one level higher than the web page with the inclusion directive you can use:

CODE
include('../header.php');

and if it's two levels higher:
CODE
include('../../header.php');

See also http://htmlhelp.com/faq/html/basics.html#relative-url

However, if you want to move web pages to another directory in the future (not really recommended though, since it breaks URLs) without rewriting their inclusion file paths you might use:

CODE
include($_SERVER['DOCUMENT_ROOT'].'/header.php');

which I think creates an absolute path in the server's file system to the directory specified as the web root. See also http://www.php.net/manual/en/reserved.vari...ariables.server

QUOTE
I tried adding a slash mark / before the header and correct directory name, and also tried inputting my entire web site url plus directory path that the include codes are in, but those did not work.

The include() statement uses the server's internal file paths, so a "/" would resolve to the server root, not the web root.



nWo Sting
QUOTE(Christian J @ Apr 14 2007, 06:26 PM) *

If the included file is one level higher than the web page with the inclusion directive you can use:

CODE
include('../header.php');

and if it's two levels higher:
CODE
include('../../header.php');




thanks, I tried that and it is not working. I am using this as a test code <?php include("inc/header.php"); ?> keeping all the include files under the "inc" directory. Now I made a few test directories, one of them is just the letter S, however the "s" directory is inside of a directory titled "flash". So its....

www.mysite.com/flash/s/testpage.php

Now I have a php page under the "s" test directory, and the includes do show up as long as I have the directory titled "inc" located inside of the "s" folder/directory. However I want to make several directories, and I want the "inc" folder to stay under "flash". I moved the "inc" directory out from inside the "s" directory so that both are under the "flash" directory. So its now....

www.mysite.com
*flash directory (and inside the flash directory are....)
1. INC
2. S (the test folder)

So the php page located under the S directory will not show the include codes once I moved the "inc" directory up one, even after I changed my inc code to <?php include("flash/inc/header.php"); ?>

See I thought that code should work because the "inc" directory with header.php is located inside the "flash" directory. So Basically everything under the "s" directory will not show the include codes. Do php includes work in reverse order or something? Or maybe its something with my .htaccess file but I cannot get the includes to work unless they are in the same exact directory.

***My htaccess currently says....

AddHandler x-httpd-php5 .php
AddHandler server-parsed shtml html htm
Christian J
QUOTE(nWo Sting @ Apr 15 2007, 02:10 AM) *

So its now....

www.mysite.com
*flash directory (and inside the flash directory are....)
1. INC
2. S (the test folder)

Then you should use

CODE
<?php include("../inc/header.php"); ?>

Assuming "testpage.php" (with the inclusion directive) is inside the "s" directory, you need to use "../" to go up one level (so you end up inside "flash"), then you can continue with the "inc/header.php" part.

QUOTE
<?php include("flash/inc/header.php"); ?>

If "testpage.php" is inside "s", the above would resolve to the nonexisting "www.mysite.com/flash/s/flash/inc/header.php".

QUOTE
See I thought that code should work because the "inc" directory with header.php is located inside the "flash" directory.

No, the relative URL is based on the location of "testpage.php" (the file with the inclusion directive). In this case that file is in "s", not in "flash".

Peter1968
Be mindful of open_basedir restrictions too. Many php setups will not allow execution of scripts unless they're in a certain place.
nWo Sting
QUOTE(Christian J @ Apr 15 2007, 03:48 AM) *

QUOTE(nWo Sting @ Apr 15 2007, 02:10 AM) *

So its now....

www.mysite.com
*flash directory (and inside the flash directory are....)
1. INC
2. S (the test folder)

Then you should use

CODE
<?php include("../inc/header.php"); ?>

Assuming "testpage.php" (with the inclusion directive) is inside the "s" directory, you need to use "../" to go up one level (so you end up inside "flash"), then you can continue with the "inc/header.php" part.


Thanks Christian, it worked. It was my fault, when I saw your original post showing "../inc" I thought the "../" meant you were telling me to type in the name of the other directory, not to actually type the periods. I'll need to make sure I pay exact attention when people type codes in the future. Thanks a lot though, you have resolved all the issues I had.
Dr Z
At this point, I will be asking for clarification on the syntax of the PHP include function:

Starting with the assumption that both the main file and included file are in the same directory, which is more correct and/or better:

1 - <?php include("abc.inc"); ?>
2 - <?php include 'abc'; ?>

From what I can understand from Example 16.7 on http://us2.php.net/manual/en/function.include.php, it is #2. (If I remember correct, sometime ago when I was tinkering with PHP, I used #2, and it worked.

Any clarification will be appreciated.
Dr Z
QUOTE(Dr Z @ Apr 16 2007, 04:31 PM) *

At this point, I will be asking for clarification on the syntax of the PHP include function:

Starting with the assumption that both the main file and included file are in the same directory, which is more correct and/or better:

1 - <?php include("abc.inc"); ?>
2 - <?php include 'abc'; ?>

From what I can understand from Example 16.7 on http://us2.php.net/manual/en/function.include.php, it is #2. (If I remember correct, sometime ago when I was tinkering with PHP, I used #2, and it worked.

Any clarification will be appreciated.


Sorry for the typo, the two possible combinations should be:
1 - <?php include("abc.inc"); ?>
2 - <?php include 'abc.inc'; ?>
Brian Chandler
QUOTE


Sorry for the typo, the two possible combinations should be:
1 - <?php include("abc.inc"); ?>
2 - <?php include 'abc.inc'; ?>


Well, the PHP manual is slightly flaky, in that it calls "include" a function, and writes it as "include()", but it's fairly clear the standard syntax is

include X;

where X is an expression that evaluates to a string, and might be a constant string, or any other of the following:

include 'fish.txt';
include "fish.txt";
include "$path/fish.txt";
include $_SERVER['DOCUMENT_ROOT'] . $filename;

Since in general if X is an expression, (X) is also another expression, it cannot be wrong to write:

include ($_SERVER['DOCUMENT_ROOT'] . $filename);

... and it's likely that omitting the space before the first left bracket ( will also be interpreted as intended.

However, I'm unclear why you are that bothered about this.

First, this is a server programming language. If you test your program and it works, that's the end. It doesn't depend on other people's configurations.

Second, this is not CSS: if include "fred.txt"; works ok, there is no chance* that it will suddenly stop working when you write include "../../fred.txt"; or whatever.

* Well, there is a chance, but it would be a bug, not a feature as in CSS.
Ronnie 268
I have the same problem.

I have a menu.php file in www.jjtcomputing.co.uk/php/

I want all php files to refer to it.

index.php in /php/ uses include("menu.php")

in /php/linux/linux.html I want to include the same file.

Is it include("/php/menu.php")
or include("../menu.php")
or what?

Regards,

Ronnie 268
Brian Chandler
QUOTE
I have a menu.php file in www.jjtcomputing.co.uk/php/

I want all php files to refer to it.

index.php in /php/ uses include("menu.php")


(Why doesn't the quoting function in this forum work properly. It glued the three lines together...?)

Anyway, you are (ab)using "/php" to be mean directory php in the *document root* directory, whereas normally "/php/" means directory php in the *file system* root directory.

But in this same [document root]/php/ directory, obviously "menu.php" refers to the file menu.php in the current directory, which is what you want.

If you want to refer to menu.php in the parent directory of the current directory, you need "../menu.php".

Etc. This is very very very basic, and it would be a good idea to understand it if you want to graduate from being a microsheep button-pusher.

pandy
I don't know it's forbidden to ask basic questions here. On the contrary, it is encouraged.

Ronnie 268, also see this recent thread. smile.gif
http://forums.htmlhelp.com/index.php?showtopic=6737&hl=
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.