在PHP中包含配置文件 [英] Including config file in PHP

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

问题描述

我正在开发一个我无法包含文件的项目。

I am developing a project where I am not able to include the files.

我的文件夹结构:

--Myproject
-----Config
----------config.php
-----Includes
----------Images
---------------image.jpg
----------CSS
---------------test.css
----------JS
---------------test.js
-----Modules
----------Home
---------------index.php
----------Contact 
----------MyPage

我正在尝试访问我的Modules / Home / index.php中Config / config.php内的配置文件

I am trying to access the config file which is inside the Config/config.php in my Modules/Home/index.php

但是我无法包含配置文件?

But I am not able to include the config file?

我试过:

1.
define("ROOT", __DIR__ ."/");

2.
define("HTTP", ($_SERVER["HTTP_HOST"] == "localhost")
   ? "http://localhost/myproject/"
   : "http://your_site_name.com/"
);
<img src="<?php print HTTP; ?>images/banner.gif">

3.
define('PROJECT_ROOT', getcwd());

4.
$_SERVER['DOCUMENT_ROOT'];

Ref: [link][2]

5.
echo $_SERVER['SERVER_NAME'];

我怎么能喜欢在文件夹结构之外但在我的项目中的config.php?

How can I like a config.php which is out side the folder structure but inside my project?

推荐答案

你可以使用绝对路径,这可能是 /Myproject/Config/config.php 或通过在上一级文件夹中导航然后转到您的require文件 ../../ Config / config重置您的目录。 php

You can either use absolute path which could be /Myproject/Config/config.php or reset your directory by navigating in upper level folder and then going to your require file ../../Config/config.php

所以你可以设法包含

include('../../Config/config.php');

中所述,包括 文档


如果定义了一个路径 - 是否绝对(在Windows上以驱动器号或\开头,在Unix / Linux系统上以
或/开头)或相对于当前目录(以。或..开头)
- include_path将被完全忽略。例如,如果文件名以
../开头,则解析器将查找父目录以查找所请求的文件。

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

已编辑

让我们分析包含路径。我们实际上在 Modules / Home / 文件夹中。 te达到根级别并且可以进入 Config 文件夹我们需要进入两级,我们可以通过 ../ 对于每个级别,所以在我们的例子中 ../../ 。现在我们在 root 目录中,我们可以浏览 Config / 并获取我们想要的文件的config.php 。现在混合所有toghter将有 ../../ Config / config.php

Let's analyze include path. We are actually in Modules/Home/ folder. te reach root level and can get inside Config folder we need to go two level upper, and we can do this by doing ../ for each level, so in our case ../../. Now that we are in root directory we can navigate through Config/ and get our desired file config.php. Now mixing all toghter will will have ../../Config/config.php.

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

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