4

I have a configuration file. This is not a stand-alone file. I will be including this file at the top of the pages that I want to use in, using require(). I want to dynamically get the complete absolute url path to this configuration file, regardless of it's location and store it as a constant within itself. For example:

Physical Location: (root dir)/my_folder/configuration.php

Need URL as: www.mydomain.com/my_folder/configuration.php

Physical Location: (root dir)/demos/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/my_folder/configuration.php

Physical Location: (root dir)/demos/site1/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/site1/my_folder/configuration.php

Physical Location: (root dir)/demos/site2/my_folder/configuration.php

Need URL as: www.mydomain.com/demos/site2/my_folder/configuration.php

Simple so far? Here's what really needed and makes it complicated (IMO). Consider this:

Config file located at: www.mydomain.com/demos/site2/my_folder/configuration.php

Have nested folders: www.mydomain.com/demos/site2/1/2/3/index.php

When I access the index.php file in the "3" sub-folder by following the URL above, I need the path to configuration file as www.mydomain.com/demos/site2/my_folder/configuration.php and not as www.mydomain.com/demos/site2/1/2/3/my_folder/configuration.php

How can I achieve the above?

Devner
  • 6,825
  • 11
  • 63
  • 104

2 Answers2

5

If you can rely on the value of $_SERVER[ 'DOCUMENT_ROOT' ], then inside configuration.php:

$path = substr( __FILE__, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) );

$url = "www.mydomain.com{$path}";

If it fits your use case, you can make it more dynamic using $_SERVER[ 'HTTP_HOST' ];

DOCUMENT_ROOT

I've used DOCUMENT_ROOT liberally in my development, as it's often the only dynamic variable available for constructing certain self-referential paths. There's a looong running Apache bug ticket (#26052) about how DOCUMENT_ROOT is poorly handled, particularly that Apache wouldn't allow you to set the value with RewriteRule and didn't set it to a sensible value when using mod_vhost_alias. The discussion goes on over a period of 7-8 years as people presumably from the Apache project resist changing the behavior, until they finally came around and made a change this year in 2.4.1. (I looked into this previously, but I forget now what the exact changes were, and how satisfying they are.)

If you look at the comments on the ticket you'll see people resisting changes to the behavior with comments like:

Don't trust the DOCUMENT_ROOT variable.

DOCUMENT_ROOT is not, never was, and never will be a reliable way of finding the filesystem path to web content.

So I suggest reading the comments on that ticket to see what people are saying the caveats of using it are. I've used it with a lot of success and don't know of a better way to achieve the same things in the same situations that DOCUMENT_ROOT is available and provides the necessary data.

Community
  • 1
  • 1
JMM
  • 26,019
  • 3
  • 50
  • 55
  • HOLY GOOD HEAVENS! How in the world did you do it so easily? I was struggling with it for a long time. Answer accepted :) Just for knowledge purpose: Is is unsafe to rely on $_SERVER[ 'DOCUMENT_ROOT' ]? What can happen? – Devner Aug 04 '12 at 04:50
  • I just knew from experience about some of the things that are available that could help (like `__FILE__` and `DOCUMENT_ROOT`) and what combination to use them in. I'm not sure what elements of this solution might be unfamiliar to you, but hopefully you won't have to struggle as much the next time you have a situation like this. I added a section to my answer re: your question about `DOCUMENT_ROOT`. – JMM Aug 04 '12 at 13:45
  • You are correct. When I checked the values of the $_SERVER superglobal, I did find that document root was fetching what I needed. I will check the URL that you have provided. +1 for the explanation. Do you recommend using anything else other than Document Root for the filesystem path from your current usage? – Devner Aug 04 '12 at 22:51
  • 1
    "Do you recommend..." -- do you mean recommend something else for your situation? I would probably just use `DOCUMENT_ROOT`, but I guess in this situation you could alternatively find the common prefix of `$_SERVER[ 'SCRIPT_FILENAME' ]` (which will reflect `index.php`) and `__FILE__` (which inside `configuration.php` will reflect `configuration.php`), then strip the prefix. You could also think about finding the prefix by stripping `$_SERVER[ 'REQUEST_URI' ]` from `$_SERVER[ 'SCRIPT_FILENAME' ]`, just make sure to strip the query string from `REQUEST_URI` if you're going to do that. – JMM Aug 04 '12 at 23:12
2

What I used, which worked perfectly for what I wanted, was this:

define('URL', dirname(substr($_SERVER['SCRIPT_FILENAME'], strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) )).'/');
laromicas
  • 21
  • 1