Path of current PHP script relative to document root

前端 未结 4 2013
一整个雨季
一整个雨季 2021-02-14 09:08

TL;DR: What alternatives to the last code sample on this page are there when trying to use relative links on pages included with PHP\'s include com

4条回答
  •  难免孤独
    2021-02-14 09:50

    What I've done in the past is to use a setup like this:

    define('DIR_BASE',     dirname( __FILE__ ).'/'); 
    define('DIR_SYSTEM',   DIR_BASE.'app/');
    etc.
    

    This would be placed in a file root of your project, which will give you access to other areas of your file structure relative to the root.

    For your case, it would look like this:

    define('DIR_BASE',     dirname( __FILE__ ).'/');
    define('DIR_CSS',      DIR_BASE.'css/');
    define('DIR_PHP',      DIR_BASE.'php/');
    define('DIR_CONTENT'   DIR_BASE.'content/');
    

    This would go in getpath.php in your root, and would give you the ability to reference any file or directory regardless of where it is placed in the directory structure (be sure to include it wherever you'll be using them). You wouldn't need to change your directory structure or anything like that, and you don't have to worry about any vulnerabilities with something like this, since it's internal.

    Edit I keep coming back to the structure of the system. Is this a multi-site system that uses the same CSS through out? If not, then what is the justification for having the directory structure laid out like this? Can you give just a little more detail please?

提交回复
热议问题