Need to use 'require' in tempate at diff levels of folders

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lwoodsinc
Forum Newbie
Posts: 8
Joined: Tue Sep 26, 2006 5:59 pm

Need to use 'require' in tempate at diff levels of folders

Post by lwoodsinc »

I want to use 'require' calls in a template that is being used for pages at different folder levels and PHP won't let you reference the 'required' file from the root. For example, I want to do the following:

require('includes/dummy.php') from the root folder but I want this same page to be used in a higher level folder, such as an 'admin' folder. For this to work I have to change the require to:

require('../includes/dummy.php');

It would be perfect if I could just use:

require('/includes/dummy.php');

but this isn't acceptable.

So, how do I do this???

Thanks.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Need to use 'require' in tempate at diff levels of folde

Post by Darhazer »

Use absolute paths

Code: Select all

require( dirname( __FILE__ ) . '/includes/dummy.php');
lwoodsinc
Forum Newbie
Posts: 8
Joined: Tue Sep 26, 2006 5:59 pm

Re: Need to use 'require' in tempate at diff levels of folde

Post by lwoodsinc »

MUCH appreciated. This has driven me nuts for a long time.
lwoodsinc
Forum Newbie
Posts: 8
Joined: Tue Sep 26, 2006 5:59 pm

Re: Need to use 'require' in tempate at diff levels of folde

Post by lwoodsinc »

I spoke too soon. Your suggestion doesn't work!!!

If the following is in the root

<?php echo dirname( __FILE__ ) . '/includes/header.php'; ?>

you will see something like this:

/home/content/xx/xxxxxx/html/dummyroot/includes/header.php

but if the same statement is in a folder such as /admin then you will see this:

/home/content/xx/xxxxx/html/dummyroot/admin/includes/header.php

And you can see that the two links aren't pointing to the same folder.

Any other ideas???
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Need to use 'require' in tempate at diff levels of folde

Post by Darhazer »

Yes, in the admin use

Code: Select all

require( dirname( __FILE__ ) . '/../includes/dummy.php');
This actually solves the problem, when the included files include other files.
If you don't use the full path, which is represented by the dirname(__FILE__), then all includes are relative to the path of the calling script, i.e.
if you are in /admin/index.php - to /admin
if you are in /index.php - to /

So in all your include files you can include other files by using the full path and there no be any difference from what path level you will use the file. But always when you include file, you have to specify the path relatively to the current script path. Does that make sense?
lwoodsinc
Forum Newbie
Posts: 8
Joined: Tue Sep 26, 2006 5:59 pm

Re: Need to use 'require' in tempate at diff levels of folde

Post by lwoodsinc »

I understand your example but I happen to be using a Dreamweaver template so the 'include' is in the template and the same template contents is merged into all of the pages regardless of whether they are in the root or up 1-x levels. If I could always reference the 'include' file path from the root then I would be O.K. but PHP won't let me.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Need to use 'require' in tempate at diff levels of folde

Post by Darhazer »

lwoodsinc wrote:I understand your example but I happen to be using a Dreamweaver template so the 'include' is in the template and the same template contents is merged into all of the pages regardless of whether they are in the root or up 1-x levels. If I could always reference the 'include' file path from the root then I would be O.K. but PHP won't let me.
Oh sorry, I didn't understand your problem - you are using a Dreamweaver template.
Well, the only think I can suggest, is to have in your template something like:

Code: Select all

if (!defined('SITE_ROOT'))
   define('SITE_ROOT', '/home/content/xx/xxxxxx/html/dummyroot/');

require(SITE_ROOT . '/includes/header.php');
Then you have to remember to change the SITE_ROOT when you are deploying the web site :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Need to use 'require' in tempate at diff levels of folde

Post by Benjamin »

:arrow: Moved to PHP - Code
Post Reply