dtot74529 2013-04-05 00:09
浏览 138

获得项目根目录

I keep getting confused when trying to include files that will have different paths in a local environment vs production.

For example, I have this in my usercontroller.php file:

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

On my local host, usercontroller.php is located in www/myproject/inc/ and config.php is in www/myproject/ (the project root directory)

This fails.

All I want is a way to define the projects root. i.e on localhost it's www/projectname and in productions it's / Since files are located inside the projectname directory on my localhost, it's causing issues on my production server.

What is the best way to simply define a base path and build my require_once from that?

i.e. require_once($basePath . 'inc/filename.php'

  • 写回答

5条回答 默认 最新

  • dongzong2017 2013-04-05 00:15
    关注

    Most projects use a config.inc.php file located in a common place where these things can be defined.

    config.inc.php

    define('ROOT_FOLDER', '/');
    define('APPLICATION_LINK', 'http://www.example.com/mysite');
    

    script.php

    require ('../config.inc.php');
    echo '<a href="' . APPLICATION_LINK . '">Go home</a>';
    
    评论

报告相同问题?