How do I set an absolute include path in PHP?

后端 未结 10 1898
别那么骄傲
别那么骄傲 2020-12-04 11:32

In HTML, I can find a file starting from the web server\'s root folder by beginning the filepath with "/". Like:

/images/some_image         


        
10条回答
  •  再見小時候
    2020-12-04 11:46

    What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;

    define( 'ROOT_DIR', dirname(__FILE__) );
    

    Then in all files, I know what the root of my project is and can do stuff like this

    require_once( ROOT_DIR.'/include/functions.php' );
    

    Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.

提交回复
热议问题