How to get root dir on PHP

前端 未结 4 578
醉话见心
醉话见心 2021-02-13 01:11

I use realpath(\'../\'), it work fine but the result is D:wampwww ( real path is D://wamp/www ). Anybody can tell me how to get realpath by the right way? Thanks you verry much.

相关标签:
4条回答
  • 2021-02-13 01:19

    You can put some php file into the root and get:

    $rootPath = dirname(__FILE__)
    

    inside it.

    0 讨论(0)
  • 2021-02-13 01:22

    If using php 5.3 or up then use

    filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');
    
    0 讨论(0)
  • 2021-02-13 01:25

    Use:

    $_SERVER['DOCUMENT_ROOT'];
    
    0 讨论(0)
  • 2021-02-13 01:41

    To get root directory path of a PHP project:

    For PHP >= 5.3.0

    use: __DIR__

    Note: The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.

    For PHP < 5.3.0

    use: dirname(__FILE__) or realpath(dirname(__FILE__))

    Or in most common for getting server document root directory where projects resides :

    $_SERVER['DOCUMENT_ROOT'] or filter_input(INPUT_SERVER, 'DOCUMENT_ROOT')
    

    See : "magical" PHP constants

    0 讨论(0)
提交回复
热议问题