The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V  1 2 >  
Reply to this topicStart new topic
> php include from a different directory?
nWo Sting
post Apr 14 2007, 05:57 PM
Post #1


Member
***

Group: Members
Posts: 63
Joined: 5-November 06
Member No.: 709



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

This post has been edited by nWo Sting: Apr 14 2007, 05:57 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 14 2007, 06:26 PM
Post #2


.
********

Group: WDG Moderators
Posts: 8,217
Joined: 10-August 06
Member No.: 7



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.



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
nWo Sting
post Apr 14 2007, 07:10 PM
Post #3


Member
***

Group: Members
Posts: 63
Joined: 5-November 06
Member No.: 709



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

This post has been edited by nWo Sting: Apr 14 2007, 07:12 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 15 2007, 03:48 AM
Post #4


.
********

Group: WDG Moderators
Posts: 8,217
Joined: 10-August 06
Member No.: 7



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

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Peter1968
post Apr 15 2007, 06:56 AM
Post #5


Serious Coder
*****

Group: Members
Posts: 448
Joined: 23-September 06
Member No.: 213



Be mindful of open_basedir restrictions too. Many php setups will not allow execution of scripts unless they're in a certain place.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
nWo Sting
post Apr 15 2007, 10:26 AM
Post #6


Member
***

Group: Members
Posts: 63
Joined: 5-November 06
Member No.: 709



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Apr 16 2007, 06:31 PM
Post #7


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Apr 18 2007, 10:03 PM
Post #8


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



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'; ?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Apr 19 2007, 02:07 AM
Post #9


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ronnie 268
post Aug 29 2008, 04:30 AM
Post #10





Group: Members
Posts: 2
Joined: 29-August 08
Member No.: 6,537



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

This post has been edited by Ronnie 268: Aug 29 2008, 04:33 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Aug 29 2008, 10:17 AM
Post #11


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



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.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Aug 29 2008, 03:08 PM
Post #12


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 18,370
Joined: 9-August 06
Member No.: 6



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=
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
GeeDave
post Sep 22 2008, 07:51 PM
Post #13





Group: Members
Posts: 4
Joined: 22-September 08
Member No.: 6,717



Heya folks, seems I'm having the same (or a similar) problem to that which is described here. Except I currently cannot resolve it. So here's the thing:

On the root level, I have a few pages including my includes, all works just fine and dandy. But now I need to make a new directory in the root folder called "tutorials", and in this directory for the moment is just a single "rigchar1.php" file.

I should note that although I have .php pages, the only real php I'm using is the include function.

So for example, i've got:

"tutorials.php" on the root, which includes "header.php", which is also on the root.

but I need "tutorials.php" to point to > "tutorials/rigchar1.php"

And this is where I get a headache. "rigchar1.php" also uses the same includes as the pages on the root. And even though it finds these include files, it is not displaying them correctly. See image for what I'm trying to say:

IPB Image

As you can see, "../header.php" is telling this file to look up a directory and find it, which it does, as it's able to find the table and image from "header.php", it's just refusing to show the image itself.

There's also the issue of my navigation getting all screwy. On the root level it works fine, but when I go into "tutorials/rigchar1.php", for some reason, it adds the "tutorials/" directory onto all of my links. Why!?

All the pages on the root level can be found here, in full working order:
http://garydave.com/Upload/gdv2/

And then the /tutorials directory, which is just not having any of it.
http://garydave.com/Upload/gdv2/tutorials/rigchar1.php

I hope to god i'm missing something simple here. Cheers for reading.

This post has been edited by GeeDave: Sep 22 2008, 07:56 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Sep 22 2008, 07:59 PM
Post #14


WDG Member
********

Group: Root Admin
Posts: 8,314
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



In the preprocessor source that I use to generate my personal site, the common elements of the page layout refer to $(TOPDIR) when they link to standard documents. $(TOPDIR) is defined as "./" in the top-level directory, as "../" in the directories below that, as "../../" in the directories below that, and so on.

Thus, the copyright links in the top-level directory link to ./siteinfo/copyright.html , those in the directory below that link to ../siteinfo/copyright.html , those in the directory below that link to ../../siteinfo/copyright.html , and so on.

It seems that a similar approach could be used here.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Sep 22 2008, 08:12 PM
Post #15


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



QUOTE(Darin McGrew @ Sep 23 2008, 09:59 AM) *

In the preprocessor source that I use to generate my personal site, the common elements of the page layout refer to $(TOPDIR) when they link to standard documents. $(TOPDIR) is defined as "./" in the top-level directory, as "../" in the directories below that, as "../../" in the directories below that, and so on.

Thus, the copyright links in the top-level directory link to ./siteinfo/copyright.html , those in the directory below that link to ../siteinfo/copyright.html , those in the directory below that link to ../../siteinfo/copyright.html , and so on.

It seems that a similar approach could be used here.



Yes, if you look six posts up, you'll see that in PHP the following always allows you to refer to files within the server document tree:

include $_SERVER['DOCUMENT_ROOT'] . $filename;
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
GeeDave
post Sep 22 2008, 08:38 PM
Post #16





Group: Members
Posts: 4
Joined: 22-September 08
Member No.: 6,717



I'm sorry, but I'm not quite following what you're both saying here. I'm certainly not a programmer and most of what I've read in this thread goes right over my head. I've only ever done HTML sites in the past and issues like this never came up. I was advised to look into php includes on another forum as they said it would definitely be easier for what I wanetd to do. Which is true, except for when I want different directories!

Without sounding rude, is there some way one of you could explain this in words that I might understand?

As for the "../" "./" "../../" stuff, I've tried all of them now, and the closest I have to anything workable is with what I already have. "../header.php" in my "rigchar1.php" file. It definitely finds the header.php, but it's not finding the images.

I read on another forum about absolute paths for images and links. What is this exactly?

Apologies for this, i'm just trying to get a basic site that works, and that will allow me to alter my navigation/header etc.

This post has been edited by GeeDave: Sep 22 2008, 08:39 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
GeeDave
post Sep 22 2008, 09:42 PM
Post #17





Group: Members
Posts: 4
Joined: 22-September 08
Member No.: 6,717



Can't seem to edit my post anymore, but thought i'd just add that the problem is no longer there. Here's what my Edit would have said if it let me do it:

"Huzzah! Crisis averted. I began by putting all the files where I eventually wanted them to be anyway, at the root of my server space, then just changed a few directories by altering "../" into "/" and the links which had no dashes, now begin with a "/". This method didn't work before, perhaps that was due to all the files not being on the root? I dunno... not fussed anymore, I can sleep easy! Cheers for your speedy replies guys, and cheers if you actually gave me a solution but I'm too dull to of noticed."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Sep 23 2008, 04:12 AM
Post #18


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



QUOTE
As for the "../" "./" "../../" stuff, I've tried all of them now, ...


A few posts up I said this was terribly basic, and something you need to know. I wasn't intending (as Pandy interpreted it) to mean people shouldn't ask elementary questions; I was trying to help by making it clear that this _is_ something elementary, which it is well worth spending the time required to understand it. Should be about 10 minutes?

Unix (and its offshoots and imitations, by and large) has this notion of a tree hierarchy, and the "current directory", which is where you are at the moment in this tree. In a web script it's normally the directory holding the file being executed.

A filename "brian.php" refers to the file of that name in the current directory.
The filename "dir/brian.php" means "go to directory 'dir' (always starting from where you are, of course) and find the file brian.php. So you go _down_ the tree (away from the root!) by adding the directory name.

To go _up_ the tree, use '..' which is the parent directory. So '../../../fred/joe/fiddlesticks.html' goes up 3, and down 2 through the specified directories.

The root ("top" - software trees are always upside down!) is always represented by '/' at the beginning.

Then there _is_ an extra complication for web servers: the files on the website start with a slash after the domain name. Then it's just the same: / means the top of the *website*, '..' means the parent (from wherever you are). But the top of the website ('/' in web addresses) is not normally (ever!) the top of the file system of the server itself. And php includes refer to filenames within the whole file system (so you can access include files which you have put outside the reach of the web server, which is a Very Good Idea, Because Fewer Things Go Wrong That Way).

Anyway, if anyone is confused by that explanation, I would try to sort out their confusion, but if people keep coming along with "I've tried '..', '../..', '/../', and '//..//./', what do I need?" I think it's a bad idea to keep supplying Answers as though they were magic potions. You know what they say about men and fish (and women and bicycles, of course)...


S
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
GeeDave
post Sep 23 2008, 02:33 PM
Post #19





Group: Members
Posts: 4
Joined: 22-September 08
Member No.: 6,717



Hey Brian, perhaps my words were a little vague, I did not mean that I'd tried that "stuff" in the sense that I didn't know what I was doing. I do understand the up and down of directories, however I did not know about the "/" simply being top level on the website. Which would explain why I was getting my issues as I was merely linking lke i'd always done with html, with no "/" preceeding the base/root page links.

I imagine this is something I should have caught wind of years ago, but I'm not aiming to learn this to any professional standard, I certainly don't build websites for other people. I'm actually an artist (3D), so by nature I'm supposed to hate any sort of code anyway! tongue.gif

Cheers for taking the time to explain all of that. Though I don't want you to think I'm a complete dunce, and your words have enlightened me! Ta.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
catherinecarter85
post Jun 1 2009, 04:07 AM
Post #20





Group: Members
Posts: 1
Joined: 1-June 09
Member No.: 8,757



Cheers and we look forward to your Forum Favourites selections! tongue.gif


pret auto
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th September 2019 - 08:23 AM