Skip to content Skip to sidebar Skip to footer

How To Use A Php Includes Across Multiple Directories/sub Directories With Relative Paths

I'm having difficulty with paths in a cms system I'm attempting to build, I've basically got a folder with my header.php and footer.php files inside. These are included in index.ph

Solution 1:

i usualy have a file called config in my application root and in it i define a constant for base path and a few others:

define('APP_BASE_PATH', dirname(__FILE__));
define('APP_FUNCTIONS_PATH', APP_BASE_PATH . '/functions');

and i include my files like

include (APP_BASE_PATH . 'includes/another_file.php');
include (APP_FUNCTIONS_PATH . '/function_file.php');

that way i can place my aplication in whatever directory, plus i can move files around without to much worries.

also using full path makes the include faster

Solution 2:

I prefer setting the environment variables (in Apache, using .htaccess or the .conf). This way you can move all your files freely anywhere in webroot and it will have access to those variables.

SetEnv lib /library/folder/
SetEnv public /my/web/root/
SetEnv environ DEVELOPMENT

Also you can use the variable named 'environ' mentioned in the above .htaccess snippet to include a server specific file as config file in all of your scripts and set various variables there.

require_once getenv('lib')."Configs/Config_".getenv('environ').".php";

Enjoy your freedom!

Solution 3:

or...

include($_SERVER['DOCUMENT_ROOT'] .'/includes/header.php');

Solution 4:

Relative and absolute paths in PHP are a bit fragile because they depend not just on the current directory of the including file, but also the current working directory.

So you need a two-part solution.

Firstly, you need a redirector. Basically, this is an include file that serves as a single-point-of-call for all other pages. Its job is to go and include the rest of your infrastructure. All your pages call this redirector and only this redirector (but you can chain them).

This redirector now does

include_once dirname(__FILE__).'/include/include.php';

This lets you change your infrastructure's include file, or location and all you have to update is one file. The dirname() call solves all the relative and absolute problems and has it look for the next step relative to itself. And by definition this only changes when you change it, so it will always work.

The second part is a custom includer so you can call content by name with a function and it goes and gets the right file. Burying this in your infrastructure directory is where is goes. It then becomes a black-box that the pages outside this area call without knowing and without needing to know how it works or where it is. That removes the need for path constants to include page fragments because you have one place doing it all for you.

Solution 5:

I have had this similar issue and posted this query in this link in SO. The URL is : Issue with PHP include with global path.

While working on the solutions given by people and looking at various threads (including this one - which I had quoted in my solution at the bottom section of my post), I had a way! I had posted the solution as well. It may help some one who is facing a similar issue.

Post a Comment for "How To Use A Php Includes Across Multiple Directories/sub Directories With Relative Paths"