[英]PHP find require_once real path
i need to require_once
this files main.class.php
and functions.php
and config.php
to admin/test.php
. 我需要require_once
这个文件main.class.php
和functions.php
和config.php
到admin/test.php
。 how to php find require real path?! 如何找到php需要真正的路径?!
NOTE: main.class.php
in class folder
, functions.php
in includes folder
and config.php
in root
directory and test.php
in admin folder
. 注意: class folder
main.class.php
, functions.php
中includes folder
root
目录中的includes folder
和config.php
以及admin folder
test.php
。
File Directory: 文件目录:
root
/admin/
->test.php
/class/
->main.class.php
/includes/
->functions.php
/templates
config.php
phpinfo.php
I would add your root
folder to the include path and work from there. 我会将您的root
文件夹添加到包含路径并从那里开始工作。
From admin/test.php
来自admin/test.php
set_include_path(implode(PATH_SEPARATOR, array(
realpath(__DIR__ . '/..'),
get_include_path()
)));
require_once 'class/main.class.php';
require_once 'includes/functions.php';
require_once 'config.php';
If I'm following your question correctly, assuming you wanted to define this in the config.php
file, you would do something like this 如果我正确地按照你的问题,假设你想在config.php
文件中定义它,你会做这样的事情
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__,
get_include_path()
)));
require_once 'class/main.class.php';
require_once 'includes/functions.php';
require_once __DIR__ . '/../config.php';
Here is a solution not depending on the $_SERVER variable: 这是一个不依赖于$ _SERVER变量的解决方案:
require_once(__DIR__.'/../class/main.class.php');
require_once(__DIR__.'/../includes/functions.php');
require_once(__DIR__.'/../config.php');
From admin/test.php
you would require like this: 从admin/test.php
你需要这样:
require_once("../class/main.class.php");
require_once("../includes/functions.php");
require_once("../config.php");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.