How to use a PHP includes across multiple director

2019-01-12 03:39发布

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!

标签: php html include
5条回答
Viruses.
2楼-- · 2019-01-12 04:06

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!

查看更多
倾城 Initia
3楼-- · 2019-01-12 04:09

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.

查看更多
混吃等死
4楼-- · 2019-01-12 04:11

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.

查看更多
狗以群分
5楼-- · 2019-01-12 04:15

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

查看更多
走好不送
6楼-- · 2019-01-12 04:25

or...

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

查看更多
登录 后发表回答