Quick question about file locations

Hey,

I have my main index.php file and in there I include a top.php and a bottom.php file which are stored in an includes folder.

As follows:

<?php require_once("includes/top.php"); ?>

and

<?php require_once("includes/bottom.php"); ?>

Now, I have added an admin folder which will store all the documents related to the sites cms, I built a page of the cms as index.php but within the admin folder and also used includes to add the top and bottom pages but included them as:

<?php require_once("../includes/top.php"); ?>

Basically just adding “…/” to the file path so it can find the files.

However…

In the top.php file I include a file named db.php also stored in the includes file like:

<?php require_once("includes/db.php"); ?>

Now, when I view my main cms page, the top and bottom files are includes fine but those files cannot find the db.php page as that icnlude link doesn;t have the “…/” in front of the pathname.

How can I get the reference to work from both the CMS folder and the main website files.

I did expect it to work but it doesn’t.

I would appreciate quick help with this if possible.

Thanks

Neil

Why don’t you try to take a look at the include path: PHP: Description of core php.ini directives - Manual , and the set_include_path() function?

I’m confused.

I’m only usingt he standard include() function. No special folders, the includes folder I ahve is one I created and is stored in a project folder under my web root.

I just can’t see how I can get the include file to find db.php from within the admin cms system and from when top.php is included in the main site outside of the admin folder.

Thanks

Neil

EDIT: So would this work:

set_include_path('../');
require_once("includes/top.php");

From your original post it seems to me that you have top.php and db.php in the same dir, right? If that is so, have you tried to include db.php from top.php simply as

include('db.php')

, without any path? I think it should work, as they are in the same dir.

Yes, that works. Thanks!

Using absolute paths is a good approach too, there are many ways to do this depending on how you have built your web application.

Systems such as APC cache dont tend to like relative paths much.

Simple example (Apache Only)

$docRoot = getenv(“DOCUMENT_ROOT”);
include $docRoot.“/includes/top.php”;

This will ensure you are always loading the file from where you expect, without the relative path nightmare.

Hello,

I have just realised when I went back to work on the main website part of this site that, I get an error as a file I have included is not being located correctly.

Just a quick question here about absolute paths:

By the way, I am currently running this sites on my local WAMP server so it’s all offline at the moment.

I ahve my webroot which is a folder called “www”. Then for each website or application I build, I add a subfolder of “www” with the name of that site.

I know I can get the document root by using the following:

$docRoot = $_SERVER[‘DOCUMENT_ROOT’];

I could do that but to make the path correct I would have to do:

$docRoot = $_SERVER[‘DOCUMENT_ROOT’].‘\projectfoldernamehere’\;

If I added at the top of each of my pages to amke sure all the includes were correct then if I were to change the name of the project folder in the future, I would ahev to go and change all references to that folder in EVERY page of code.

Is there a way for it to find the project folder name automatically?

Thanks

Neil

Any help from anyone?

I would have to see how your files are set out, to see where you are coming from.

We use our own framework so all execution runs through the main script file triggered by index.php, so this makes it very easy to handle paths.

Here:

www - project 1 - index.php
                       - includes - top.php
                                     - bottom.php
                                     - db.php
                       - css - style.css
        - project 2 - index.php
                       - includes - top.php
                                     - bottom.php
                                     - db.php
                       - css - style.css
        - project 3 - index.php
                       - includes - top.php
                                     - bottom.php
                                     - db.php
                       - css - style.css

I know it’s not code but that keeps the white space

That’s an example of my folder structure

If your calling scripts are all in the root folder you could try this method.

index.php (or any script)

define(‘PROJECT_ROOT’, dirname(FILE));

include PROJECT_ROOT.‘/includes/top.php’;
include PROJECT_ROOT.‘/includes/db.php’;
// Do your jingle here

include PROJECT_ROOT.‘/includes/bottom.php’;

This will take the parent folder and define a global const for it.

Hope this helps.

but would I ahve to redefine that on my other pages as well as on index.php?

Another problem is that I sometimes have an admin folder in the project folder which I basically run as a separate site but it still includes both the top and bottom php include files from outside of the admin folder. How would this work for those? as this was what this topic was originally about?

If you have the subfolder admin in your project you can simply do the following


//admin.php

define('PROJECT_ROOT', dirname(dirname(__FILE__));

include PROJECT_ROOT.'/includes/top.php';
include PROJECT_ROOT.'/includes/db.php';
// Do your jingle here

include PROJECT_ROOT.'/includes/bottom.php';

Right, I’ll give it a go, it does seem an overly long way to go for something like this.

Basically, how does everyone else deal with this issue?

Another problem I ahve discovered is that, my dbd.php page is included in my top.php page whcih is itself included at the top of all my site pages. I included db.php in top.php as to me that’s the most logical place for it to be included.

now, the top.php pahe is being included in site pages stored in the project root folder AND ALSO in site pages in the admin folder stored within the project root folder. If I get db.php including correctly from the main sites perspective, as soon as you open a page from the admin areas perspective, it has an error as it cannot be located!!!

Right, As I can’t seem to figure out another solution that will work I am settling with doing the following:

Specifying the $projectRoot variable at the top of each of my main pages:

$projectRoot = dirname(__FILE__);

and doing any includes like:

require_once($projectRoot.'/includes/top.php');

and specifying the $projectRoot from inside other subfolders as:

$projectRoot = dirname(dirname(__FILE__));

Main downside is that I have to remember to put in that $projectRoot variable at the top of every page of my site.

Thanks

Neil