How to get DOCUMENT_ROOT to work on both localhost and web host?

梦想的初衷 提交于 2019-12-02 01:28:27
Shoban

Check this out Determining Plugin and Content Directories WP already has this.

So it looks like the OP would be interested in either the WP_CONTENT_DIR or WP_PLUGIN_DIR predefined constants.

This is for Wordpress installs. But for non-WP situations, I've found the following snippet to be useful:

$docroot = realpath((getenv('DOCUMENT_ROOT') && ereg('^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))), realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))));

This is in similar situations where I've wanted things to work on both a localhost/dev environment and a server environment, where the docroot locations differed. BUT even more important is that this works in the situations where I need to run a PHP file standalone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!