$_SERVER['document_root'] returns /htdocs not /public_html

和自甴很熟 提交于 2019-12-07 07:08:49

问题


I'm looking to switch over to a new host, and they provide this nice little "temporary url" to test out your files before you switch. All fine and dandy. So I copy over all of my files. At the top of every page I require another file from the server that is stored at public_html/includes/head.php. Now for whatever reason, the $_SERVER['document_root'] var is returning /public_html/htdocs/includes/head.php (which does not exist on the server) and not /public_html/includes/head.php (which does exist). The resulting error is as follows:

Warning: require_once(/home/secure31/public_html/htdocs/includes/head.php) 
[function.require-once]: failed to open stream: Permission denied in 
/home/stephe80/public_html/index.php on line 6

The guilty code:

require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/head.php');

I am inclined to think that this is an error associated with their temporary url setup, but I do not want to transfer over my DNS and find that all of my files are broken. I could change them to absolute paths, but I am curious as to whether I am missing something. Any insight would be appreciated. Thanks!


回答1:


I'd suggest to use the __FILE__ constant and work out your htdocs folder from there. It's much more reliable than relying on the $_SERVER superglobal, since that actually differs from server to server.

Example (that you'd call in your index.php in root folder):

$htdocs = str_replace(basename(__FILE__), __FILE__, '');
define('ROOT_FOLDER', $htdocs);
require_once(ROOT_FOLDER . '/includes/head.php');



回答2:


For future reference (if anyone has similar issues), it turns out that the problem was with their test server. I launched it with my code as-was (risky, I know), and all turned out well.



来源:https://stackoverflow.com/questions/12770692/serverdocument-root-returns-htdocs-not-public-html

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