OK, I have defined a PHP constant that points to a file on the server like this:
define('SOME_FILE', str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) . '/foo-bar.txt')));
SOME_FILE
will contain a full path + filename which is useble in PHP functions like include
, fopen
, etc. Now if I want to use this path as an URL in my (X)HTML output, I'm screwed. To do so, I need to strip off the document root.
Usually, $_SERVER['DOCUMENT_ROOT']
is set in 99% of PHP environments, but it can happen where it's poorly configured, overwritten by the user or even missing.
Now what I want to do is to define()
another PHP constant based on SOME_FILE
that will point to the actual URL of the file (the document root path is stripped off).
I know I can do this in 3-4 lines of code with __FILE__
, $_SERVER['SCRIPT_NAME']
(SCRIPT_NAME is required as per CGI 1.1 specification but not DOCUMENT_ROOT), strrpos
, and substr
, but I'm looking for a "one line" solution to use in a define statement...
I use PHP 5.1.0+. Amy suggestions welcome!