如何在具有相对路径的多个目录/子目录中使用 PHP 包含 [英] How to use a PHP includes across multiple directories/sub directories with relative paths

查看:28
本文介绍了如何在具有相对路径的多个目录/子目录中使用 PHP 包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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!

解决方案

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

这篇关于如何在具有相对路径的多个目录/子目录中使用 PHP 包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆