tags:

views:

709

answers:

4

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.php and work fine. But then when I attempt to use the same includes in a file within my admin sub directory, the images and CSS are broken, obviously because the relative path is now wrong.

So my question is, how can I overcome this?

After reading some of the other questions on here and various other sources, I think absolute paths are the way forward, but I've always used relative paths, so the various concepts of using config files to specify an absolute path are confusing me.

I usually manage to work things out for myself, but it's been a long day and Im stumped!

+6  A: 

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

solomongaby
Ok, so i define the constant path to my root directory and store that file(config.php or similiar) in my applications root rirectory.I then have to include the config.php file in all my files, that way I can call the constant and have the root path available to me.I can then use the root path to link to my css and images, that way my header.php and footer.php inclides will work in any directory?
Echoofali
A: 

or...

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

catfarm
only works if you install the aplication in the root path
solomongaby
True, but considering he was using relative paths to begin with I'm pretty sure this still answers the question, and with brevity no less.
catfarm
+3  A: 

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!

Anshul
Hi thanks SetEnv save my day. Php CGI unable to use php_value include_path in htaccess file. setting php include path globally is impossible from my googling. So This SetEnv AppPath "/home/account/" can be use in all php include getenv('AppPath').'includes/*.php'. This is better than using php.ini or set_include_path() or ini_set() which difficult to maintain.
CallMeLaNN
+1  A: 

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.

staticsan