How do I format a PHP include() absolute (rather than relative) path?

前端 未结 9 951
情书的邮戳
情书的邮戳 2020-12-06 04:48

On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.

What single command c

9条回答
  •  被撕碎了的回忆
    2020-12-06 05:14

    From the perspective of PHP root is the top of the file system on the web server, not the root from the perspective of the web browser.

    Most people do one of the below.

    Define a constant, in a global configuration file, and use that in each call to require/include.

    Or they use code like this.

    require_once realpath(dirname(__FILE__).'/config.php');
    require_once realpath(dirname(__FILE__).'/lib/Database.php');
    

    Using the environmental variables may be dangerous in some cases and be the source of security issues.

提交回复
热议问题