How do you know the correct path to use in a PHP require_once() statement

后端 未结 9 2124
别跟我提以往
别跟我提以往 2021-01-31 22:02

As many do I have a config.php file in the root of a web app that I want to include in almost every other php file. So most of them have a line like:

require_on         


        
9条回答
  •  孤城傲影
    2021-01-31 23:02

    If you have sufficient access rights, try to modify PHP's include_path setting for the whole site. If you cannot do that, you'll either have to route every request through the same PHP script (eg. using Apache mod_rewrite) or you'll have to use an "initialization" script that sets up the include_path:

    $includeDir = realpath(dirname(__FILE__) . '/include'); 
    ini_set('include_path', $includeDir . PATH_SEPARATOR . ini_get('include_path'));
    

    After that file is included, use paths relative to the include directory:

    require_once '../init.php'; // The init-script
    require_once 'MyFile.php'; // Includes /include/MyFile.php
    

提交回复
热议问题