How can I get the “application root” of my URL from PHP?

后端 未结 6 806
眼角桃花
眼角桃花 2020-12-09 09:06

If my URL is http://www.server.com/myapp/stuff/to/see and my directory structure on disk is htdocs/myapp/*, how can I extract the /myapp

6条回答
  •  佛祖请我去吃肉
    2020-12-09 09:42

    I incurred a similar issue. Basically, in PHP if a file 'A.php' uses relative path to refer a resource, then the current path location used as a prefix to the relative path is of the top parent php under which 'A.php' is included.

    This won't be a problem if A.php itself is top parent or when A.php sits at the same folder as is its top parent. But it will break the import otherwise.

    The best solution I found was to make the reference explicitly absolute by using DIR (which gives current location of the file) and then going to the referred by from this location for e.g. DIR."/../help.php" to refer to a file help.php sitting 1 level above A.php.

提交回复
热议问题