$_SERVER[‘DOCUMENT_ROOT’], You’ve Done Me Wrong

$_SERVER[‘DOCUMENT_ROOT’] is good for getting the full path to your websites root folder, but depending on the DocumentRoot directive in httpd.conf you may or may not have a forward slash at the end of the returned url.  This always annoys me when developing locally and everything is working.  Then you upload and test on the production system, and oops, the file path is wrong because of a missing forward slash.

Adding the following line at the top of the code will fix this problem, by making sure that the forward slash is always there, no matter where the code is being executed.

$_SERVER['DOCUMENT_ROOT'] .= ( substr($_SERVER['DOCUMENT_ROOT'],-1) == '/' ) ? '' : '/';

If you are using a common includes file in top of all your pages, you can add this line once, and it will fix the problem globally.

Scroll to Top