Making php includes work in a sub-directory

后端 未结 4 1562
北海茫月
北海茫月 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条回答
  •  遥遥无期
    2020-12-29 13:27

    Easiest way would be to use absolute pathes / URLs.

    For the URLs, define a constant/variable somewhere, that points to the root of your application, like :

    define('ROOT_URL', 'http://www.example.com');
    

    or

    $root_url = 'http://www.example.com';
    

    And use it in every link, like :

    blah
    

    This way, always OK (and the day you install your project on another server, or in a subdirectory, you only have one constant/variable to modify, and everything still works)

    For includes / requires, always use absolute pathes too ; one solution is to use dirname, like this :

    include dirname(__FILE__) . '/my_file.php';
    include dirname(__FILE__) . '/../my-other-file.php';
    

    __FILE__ is the current file, where you are writing this line ; dirname gets the path (the full path) to the directory containing that file.

    With that, you never have to worry about the relative paths of your files.

提交回复
热议问题