包括在PHP中使用相对路径的文件? [英] Including files using relative paths with PHP?

查看:78
本文介绍了包括在PHP中使用相对路径的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发网站时,我通过调用一个名为include的文件来调用php文件中的所有includes.

When developing my website I called all the includes in my php files by calling one single file called includes.

此文件的代码如下所示:(我从Lynda教程改编而成)

The code of this file looked somethig like this: (I adapted it from a Lynda tutorial)

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
    define('SITE_ROOT', 'C:'.DS.'wamp'.DS.'www'.DS.'ArmyCreator');

defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
defined('PUB_PATH') ? null : define('PUB_PATH', SITE_ROOT.DS.'public');


// load config file first
require_once(LIB_PATH.DS."helper".DS.'config.php');

现在,由于我正在部署Webiste,所以我不知道如何声明SITE_ROOt以使其正常工作?

Now since I am deploying my webiste I can't figure out how to declare the SITE_ROOt to make it work properly?

编辑

这样的代码是否正常?require_once("../../includes/helper/initialize.php");一旦部署到网站就不再起作用了?

Is it normal for code like this: require_once("../../includes/helper/initialize.php"); to not work anymore once I deploy to the website?

推荐答案

您可以通过执行以下操作来包含相对于includes.php目录的文件:

You can include the files relative to includes.php's directory by doing:

<?
$basePath = dirname(__FILE__);
require_once($basePath . "relative/path/from/basePath"); // replace the string with your real relative path

这篇关于包括在PHP中使用相对路径的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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