Need to use 'require' in tempate at diff levels of folders
Moderator: General Moderators
Need to use 'require' in tempate at diff levels of folders
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.
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
Use absolute paths
Code: Select all
require( dirname( __FILE__ ) . '/includes/dummy.php');Re: Need to use 'require' in tempate at diff levels of folde
MUCH appreciated. This has driven me nuts for a long time.
Re: Need to use 'require' in tempate at diff levels of folde
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???
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
Yes, in the admin use
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?
Code: Select all
require( dirname( __FILE__ ) . '/../includes/dummy.php');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
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
Oh sorry, I didn't understand your problem - you are using a Dreamweaver template.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.
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');