Multiple directories includes

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
idllc
Forum Newbie
Posts: 5
Joined: Thu Dec 04, 2008 8:51 am

Multiple directories includes

Post by idllc »

Here is my scenario...
I am using an includes to pull details to various pages of my site, for example
The index.php page includes url/site/header.php That works fine but now I am working on internal pages in site so for example url/pages/page1.php needs to include url/site/header.php . I cannot seem to get a grasp on pulling from one directory to another. Pulling from the root is no problem. I understand the concept of using ../ to get from a subdirectory to the root and ../../ if i am two levels deep etc but how do I go from one subdirectory to the next?
I hope that makes sense.
I have tried the following:
<?php include '../../config/header.php'; ?>
<?php include '../config/header.php'; ?>
<?php include '/home/user/public_html/config/header.php'; ?>

Thanks in advance!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Multiple directories includes

Post by RobertGonzalez »

You just go down and then up again. Say your tree looks something like this:

Code: Select all

public_html/
  config/
  includes/
  sitefiles/
Now say your app code lives in the sitefiles directory and a file within that directory needs to include a file from config and a file from includes. You would do something like:

Code: Select all

<?php
// cwd() might be something like /home/~username/public_html/sitefiles
require_once '../config/config.php'; // This one is in config/
require_once '../includes/db.php'; // This one is in includes/
require_once 'helper.php'; // This one is the current directory
?>
idllc
Forum Newbie
Posts: 5
Joined: Thu Dec 04, 2008 8:51 am

Re: Multiple directories includes

Post by idllc »

Everah wrote:You just go down and then up again. Say your tree looks something like this:

Code: Select all

public_html/
  config/
  includes/
  sitefiles/
Now say your app code lives in the sitefiles directory and a file within that directory needs to include a file from config and a file from includes. You would do something like:

Code: Select all

<?php
// cwd() might be something like /home/~username/public_html/sitefiles
require_once '../config/config.php'; // This one is in config/
require_once '../includes/db.php'; // This one is in includes/
require_once 'helper.php'; // This one is the current directory
?>
I gave it a run and I still throw errors. Oddly it does not pull style sheet either which is in the header.php.

Code: Select all

<?php
 
require_once '../config/config.php'; 
require_once '../config/common.php'; 
require_once '../site/header.php'; 
?>
 
 
 
<div style="text-align: center;">Google Ad Here</div>
</div>
<div id="wrapper">
<div id="wbt"></div>
<div id="wb">
<div id="content">
<div class="post" id="post-1">
<div class="post-head"><br />
<h2><a href="http://<?php echo $url; ?>"
 rel="bookmark" title="<?php echo $title; ?>"><?php echo $title; ?></a></h2>
</div>
<br />
<?php require_once '../nav/mainads_top.php'; ?>
 
<div style="text-align: center;"></div>
<p style="text-align: center;"><?php  $XMLFILE = "http://answers.yahoo.com/rss/searchq?p=$query" ;  $TEMPLATE = "../functions/main.html" ;  $MAXITEMS = "100" ;  include ( "../functions/rss2.php" ) ;  ?></p>
 
<?php require_once '../nav/mainads_bottom.php'; ?>
 
 
</div>
</div>
<br />
</div>
<?php
 
require_once '../nav/sidebar_A.php'; 
require_once '../nav/sidebar_B.php';
require_once '../site/footer.php'; 
?>
 
Errors example is:

Code: Select all

Warning: include(nav/categories.php) [function.include]: failed to open stream: No such file or directory in /home/affiliat/public_html/demos/templates/3R_Lease/nav/sidebar_B.php on line 19
The error for the header.php call went away but its not pulling it.
The tree is public html/index.php
public html/site/header.php
public html/config/config/.php
public html/pages/page1/.php
So if that makes sense I am trying to pull the same header info into page 1.php as I am pulling into index.php. Even more oddly is the calls in the feed you see...it pulls just using ../ and the main.html for example is also in its own directory publichtml/functions/main.html
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Multiple directories includes

Post by RobertGonzalez »

The error you are getting, if you look closely, is for another include from a file that you are including. You need to make sure that all paths resolve properly. The error is telling you that an include you are including is trying to include something else that is no longer in the expected path.

As for stylesheets, those will have to be mapped a little different because of how browsers handle file paths.
idllc
Forum Newbie
Posts: 5
Joined: Thu Dec 04, 2008 8:51 am

Re: Multiple directories includes

Post by idllc »

Ok I am much further along in trying to figure this out. I will try to summarize.
I have modularized my site and put the header in its own file...publichtml/site/header.php In the header is the includes statements for the config/common.php and config/config.php files. Now I have pages in a directory such as pages/page1.php. It is an exact duplicate of the home page with only textual content changing. It too needs to pull the config/common.php and config/config.php as well. If I were pulling these solo I would use ../ in the includes statement but since the original call is in the header this will not work. What is happening is the pages/page1.php is pulling the header data fine but when it reads the includes in the site/header.php it is reading it as though those includes statements were within its own file therefore reading the wrong path since the site/header.php does not use ../ . So my question would be is how would I define a base url to be read site wide by all files using includes.
Hope that makes some sense. Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Multiple directories includes

Post by RobertGonzalez »

Have a look at realpath() and use that as the path to your document root (another thing to try is $_SERVER['DOCUMENT_ROOT']). Set that in the root level of your app then use that var as a prepend for all other files.

Code: Select all

<?php
$path = realpath('.');
 
require_once 'config/common.php';
?>
config/common.php

Code: Select all

 
<?php
require_once $path . 'includes/main.php';
?>
Something like that. This is no exact. You should play around with the idea until you get what you are after.
idllc
Forum Newbie
Posts: 5
Joined: Thu Dec 04, 2008 8:51 am

Re: Multiple directories includes

Post by idllc »

Everah wrote:Have a look at realpath() and use that as the path to your document root (another thing to try is $_SERVER['DOCUMENT_ROOT']). Set that in the root level of your app then use that var as a prepend for all other files.

Code: Select all

<?php
$path = realpath('.');
 
require_once 'config/common.php';
?>
config/common.php

Code: Select all

 
<?php
require_once $path . 'includes/main.php';
?>
Something like that. This is no exact. You should play around with the idea until you get what you are after.
Appreciate your patience and assistance. I will play around with this. Thanks again.
Post Reply