问题
I am developing a project where I am not able to include the files.
My Folder Structure:
--Myproject
-----Config
----------config.php
-----Includes
----------Images
---------------image.jpg
----------CSS
---------------test.css
----------JS
---------------test.js
-----Modules
----------Home
---------------index.php
----------Contact
----------MyPage
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?
I tried:
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'];
How can I like a config.php which is out side the folder structure but inside my project?
回答1:
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
So you can manage to include with
include('../../Config/config.php');
As stated in include
documentation
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.
Edited
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
.
回答2:
i think this will work:
include '../../Config/config.php';
回答3:
What you could do, is include the following lines to your index.php path :
define("LOCAL_PATH_MODULES", dirname(__DIR__));
define("LOCAL_PATH_APP", dirname(LOCAL_PATH_MODULES));
define("LOCAL_PATH_CONFIG", MODULES_PATH . DIRECTORY_SEPARATOR . 'config');
require LOCAL_PATH_CONFIG . DIRECTORY_SEPARATOR . 'config.php';
What you should do, is put a file named bootstrap.php
in your Modules
folder.
Instead of the code hereabove, add the following code to your index file.
define("LOCAL_PATH_BOOTSTRAP", dirname(__DIR__));
require dirname(__DIR__) . 'bootstrap.php';
Now, add this to your bootstrap file :
// -----------------------------------------------------------------------
// DEFINE SEPERATOR ALIASES
// -----------------------------------------------------------------------
define("URL_SEPARATOR", '/');
define("DS", DIRECTORY_SEPARATOR);
define("PS", PATH_SEPARATOR);
define("US", URL_SEPARATOR);
// -----------------------------------------------------------------------
// DEFINE ROOT PATHS
// -----------------------------------------------------------------------
define("RELATIVE_PATH_ROOT", '');
define("LOCAL_PATH_ROOT", $_SERVER["DOCUMENT_ROOT"]);
define("HTTP_PATH_ROOT",
isset($_SERVER["HTTP_HOST"]) ?
$_SERVER["HTTP_HOST"] : (
isset($_SERVER["SERVER_NAME"]) ?
$_SERVER["SERVER_NAME"] : '_UNKNOWN_'));
// -----------------------------------------------------------------------
// DEFINE RELATIVE PATHS
// -----------------------------------------------------------------------
define("RELATIVE_PATH_APP", dirname(LOCAL_PATH_BOOTSTRAP));
define("RELATIVE_PATH_LIBRARY", RELATIVE_PATH_APP . DS . 'Lib');
define("RELATIVE_PATH_ADMIN", RELATIVE_PATH_APP . DS . 'Admin');
define("RELATIVE_PATH_CONFIG", RELATIVE_PATH_APP . DS . 'Config');
define("RELATIVE_PATH_MODULES", RELATIVE_PATH_APP . DS . 'Modules');
define("RELATIVE_PATH_ASSET", RELATIVE_PATH_APP . DS . 'Includes');
define("RELATIVE_PATH_ASSET_IMG", RELATIVE_PATH_ASSET . DS . 'Images');
define("RELATIVE_PATH_ASSET_CSS", RELATIVE_PATH_ASSET . DS . 'CSS');
define("RELATIVE_PATH_ASSET_JS", RELATIVE_PATH_ASSET . DS . 'JS');
// -----------------------------------------------------------------------
// DEFINE LOCAL PATHS
// -----------------------------------------------------------------------
define("LOCAL_PATH_APP", LOCAL_PATH_ROOT . RELATIVE_PATH_APP);
define("LOCAL_PATH_LIBRARY", LOCAL_PATH_ROOT . RELATIVE_PATH_LIBRARY);
define("LOCAL_PATH_ADMIN", LOCAL_PATH_ROOT . RELATIVE_PATH_ADMIN);
define("LOCAL_PATH_CONFIG", LOCAL_PATH_ROOT . RELATIVE_PATH_CONFIG);
define("LOCAL_PATH_MODULES", LOCAL_PATH_ROOT . RELATIVE_PATH_MODULES);
define("LOCAL_PATH_ASSET", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET);
define("LOCAL_PATH_ASSET_IMG", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_IMG);
define("LOCAL_PATH_ASSET_CSS", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_CSS);
define("LOCAL_PATH_ASSET_JS", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_JS);
// -----------------------------------------------------------------------
// DEFINE URL PATHS
// -----------------------------------------------------------------------
define("HTTP_PATH_APP", HTTP_PATH_ROOT . RELATIVE_PATH_APP);
define("HTTP_PATH_LIBRARY", false);
define("HTTP_PATH_ADMIN", false);
define("HTTP_PATH_CONFIG", false);
define("HTTP_PATH_MODULES", false);
define("HTTP_PATH_ASSET", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET);
define("HTTP_PATH_ASSET_IMG", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_IMG);
define("HTTP_PATH_ASSET_CSS", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_CSS);
define("HTTP_PATH_ASSET_JS", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_JS);
Now, you have a constant for both the local and HTTP variant of the most important paths in your application.
See the PHP PowerTools Boilerplate for a demo of this bootstrapping technique.
来源:https://stackoverflow.com/questions/16623350/including-config-file-in-php