Page 1 of 1

How to use includes and subfolders together?

Posted: Fri Jul 09, 2010 6:53 pm
by semmelbroesel
Hi.
I have taken over managing someone's site after giving myself a PHP crash course, and the folder structure and coding is horrific!
The worst part is that the original programmer wasn't much better than I at handling subfolders, so they pretty much hard coded all links with the complete server path, and I'm trying to clean that up.

The problem I am running into is that PHP seems to always start the path to a folder from the original PHP file and to ignore any other paths that includes are in.

Example:

Start page:
site.com/index.php
Includes folder:
site.com/includes/
Pictures folders:
site.com/images/... (with sub folders)

So far no problem, but the site also has subfolders, e.g.:

site.com/products/index.php

which accesses the same include files.

Also no problem - except that now all the paths that are listed in the include files are one folder off, especially the picture paths.

What is the standard solution for this type of situation where the includes and images stay in the same folder but the PHP files that are directly linked to switch between different sub folders?

I stumbled across this solution:

Code: Select all

//  SET PATHS FOR INCLUDE FILES
$_PATHS["base"]      = dirname(__FILE__) . "/";
$_PATHS["includes"]  = $_PATHS["base"] . "includes/";
ini_set("include_path",
        "$_PATHS[includes]");
//  END PATHS
But that also only helps me so far, and it seems as if it has to be reset in every main PHP file.

I bet for a professional this is a fairly easy one, so hopefully someone here has an answer for me.

Thanks so much in advance!

Re: How to use includes and subfolders together?

Posted: Fri Jul 09, 2010 7:19 pm
by requinix
When you need a path in PHP, use $_SERVER["DOCUMENT_ROOT"] as the location of site.com/. That gives you a path that will work everywhere, and as an added bonus will even work if you switch to another web server (probably).

Example:

Code: Select all

echo $_SERVER["DOCUMENT_ROOT"] . "/products/index.php";

Re: How to use includes and subfolders together?

Posted: Fri Jul 09, 2010 7:29 pm
by semmelbroesel
Thanks for your fast answer!

So are you recommending that all Paths that I use (i.e. links, includes, image paths etc.) all contain $_SERVER["DOCUMENT_ROOT"], and I specify the actual path from there?

E.g.

Code: Select all

<?php
include($_SERVER["DOCUMENT_ROOT"].'/functions.php');
print '<a href="' . $_SERVER["DOCUMENT_ROOT"] . '/index.php">Home</a>';
?>
Is that correct?

I might use that on the production server, but at the moment I am testing it on another site inside a subfolder:

othersite.com/testfolder/index.php
othersite.com/testfolder/includes/
othersite.com/testfolder/images/
etc.

So if I use this solution, I'll have to add "/testfolder/" to every path for now and remove it before sending it all to the production server later. Not too hard with a text editor that does "Replace in Files" :-)

Just checking if there is by any chance another solution I could try, too.

Re: How to use includes and subfolders together?

Posted: Fri Jul 09, 2010 8:19 pm
by requinix

Code: Select all

<?php
include($_SERVER["DOCUMENT_ROOT"].'/functions.php');
print '<a href="/index.php">Home</a>';
?>
$_SERVER["DOCUMENT_ROOT"] only makes sense to PHP and other stuff that's actually running on your server.
For the user's browser (that is, for links, images, stylesheets, etc.) the equivalent is a leading slash.


For your testfolder problem there are a few ways around it. Most involve defining a constant somewhere such as in a standard "functions.inc" or "common.php" file that gets included everywhere. Like

Code: Select all

<?php
// SITE_ROOT = the root directory for the website (eg, /var/www or C:\inetpub)
// HTML_ROOT = the root URL for the website (eg, empty or /newsite)

// production
//define("SITE_ROOT", $_SERVER["DOCUMENT_ROOT"]);
//define("HTML_ROOT", "");
// development
define("SITE_ROOT", $_SERVER["DOCUMENT_ROOT"] . "/testfolder");
define("HTML_ROOT", "/testfolder");
?>
Then

Code: Select all

<?php
include(SITE_ROOT.'/functions.php');
print '<a href="' . HTML_ROOT . '/index.php">Home</a>';
?>
There are also a couple potentially easier options.
  • __FILE__ is the full path to whatever PHP file you put that in. Combine it with dirname to get the directory containing the file, and then build relative paths from there.
  • If you're running PHP 5.3+ then you can simply use __DIR__ instead of dirname(__FILE__)

Re: How to use includes and subfolders together?

Posted: Sat Jul 10, 2010 9:43 am
by semmelbroesel
Thanks again for the reply.

These are all good suggestions I'll play around with. The constant sounds like my best option so far.

Just one more thing:

Do you have additional suggestions specifically on how to handle include files that are called from PHP files from different subfolders?

Example:
[text]Folder structure:
/
-> images
-> includes[/text]
index.php calls includes/include.php which has links to pictures to images/pic*.gif
products/index.php calls the same includes/include.php which has the same links to pictures images/pic*.gif but they resolve to products/images/pic*.gif because of the main PHP file that calls the include file.

Do you have a way around this, or will I have to have all links always include a "/" at the beginning (or a hard coded URL in a constant as you suggested)?

I'm just thinking that there's gotta be a simpler solution to this than hard code all URLs - how is this exact situation handled in a professional environment where you have tons of sub folders? By hard coding all URLs?

Sorry if I'm repeating myself, I just want to make sure - and I won't find an answer on how the pros handle this type of stuff in any of my "Learn PHP in xx hours/days/weeks" books... So I appreciate your help all the more!

Re: How to use includes and subfolders together?

Posted: Sat Jul 10, 2010 3:35 pm
by requinix
semmelbroesel wrote:or will I have to have all links always include a "/" at the beginning (or a hard coded URL in a constant as you suggested)?

I'm just thinking that there's gotta be a simpler solution to this than hard code all URLs - how is this exact situation handled in a professional environment where you have tons of sub folders? By hard coding all URLs?
Pretty much, yeah.

Re: How to use includes and subfolders together?

Posted: Sat Jul 10, 2010 5:44 pm
by semmelbroesel
tasairis wrote:Pretty much, yeah.
Hehe, OK, I somewhat suspected that...
I went through some of the code on the page and simply started applying the stuff you sent so far, and after a while I realized that hard coding the URL like this is pretty much the only thing that does make sense. It sure clutters up the code a little, but not enough to make it impossible to edit later.

Thank-you so much for all your help! Now I'm truly ready to give this site a major make-over! :D