PHP找到require_once真实路径 [英] PHP find require_once real path

查看:127
本文介绍了PHP找到require_once真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要require_once此文件main.class.phpfunctions.php以及config.phpadmin/test.php.如何找到所需的PHP路径真实!

i need to require_once this files main.class.php and functions.php and config.php to admin/test.php. how to php find require real path?!

注意:class folder中的main.class.phpincludes folder中的functions.phproot目录中的config.phpadmin folder中的test.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.

文件目录:

root
    /admin/
        ->test.php 
    /class/
        ->main.class.php
    /includes/
        ->functions.php
    /templates
    config.php
    phpinfo.php

推荐答案

我将您的root文件夹添加到包含路径并从那里开始工作.

I would add your root folder to the include path and work from there.

来自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';

更新

如果我正确地遵循了您的问题,假设您想在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

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

require_once __DIR__ . '/../config.php';

这篇关于PHP找到require_once真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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