Making php includes work in a sub-directory

后端 未结 4 1558
北海茫月
北海茫月 2020-12-29 12:57

Ok, I am creating an admin interface for my custom blog at the url /admin.

Is it possible for me to be able to use the same includes (including autoload), as the roo

4条回答
  •  梦毁少年i
    2020-12-29 13:42

    The best practice for this is to define a 'ABSOLUTE_PATH' constant that contains the directory that everything is located under. After that, you can simply copy and paste everything, because it is defining the 'full' path, which doesn't change from directory to directory.

    Example

    define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
    
    or
    
    define("ABS_PATH", dirname(__FILE__));
    // This defines the path as the directory the file is in.
    

    Then at any point you can simply do this to include a file

    include(ABS_PATH . "/path/to/file");
    

提交回复
热议问题