Page 1 of 1

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

Posted: Sun Jun 20, 2010 5:03 pm
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.

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

Posted: Sun Jun 20, 2010 5:18 pm
by Darhazer
Use absolute paths

Code: Select all

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

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

Posted: Sun Jun 20, 2010 5:35 pm
by lwoodsinc
MUCH appreciated. This has driven me nuts for a long time.

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

Posted: Sun Jun 20, 2010 6:13 pm
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???

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

Posted: Sun Jun 20, 2010 6:49 pm
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?

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

Posted: Sun Jun 20, 2010 6:59 pm
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.

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

Posted: Sun Jun 20, 2010 7:29 pm
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 :)

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

Posted: Mon Jun 21, 2010 8:24 am
by Benjamin
:arrow: Moved to PHP - Code