简体   繁体   English

PHP找到require_once真实路径

[英]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.phpfunctions.phpconfig.phpadmin/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.phpfunctions.phpincludes folder root目录中的includes folderconfig.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';

Update 更新

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文件中定义它,你会做这样的事情

config.php config.php文件

set_include_path(implode(PATH_SEPARATOR, array(
    __DIR__,
    get_include_path()
)));

require_once 'class/main.class.php';
require_once 'includes/functions.php';

admin/test.php 管理员/ test.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM