Question

I'm having some issues with defined absolute paths in PHP. I define the SITE_ROOT and APP_PATH like so:

defined('SITE_ROOT') ? null : 
define('SITE_ROOT', str_replace('//','/',dirname(__FILE__)) );

defined('APP_PATH') ? null : define('APP_PATH', SITE_ROOT.DS.'application');

When using the APP_PATH in an application like so:

echo APP_PATH;

...this is what I get:

/Users/user/Sites/MyWebsite/application

What I want is for the output to be:

localhost/application

Is there any non-kludgy way of doing this?

My use case is to use the APP_PATH (already in use for doing all my require()'s) echoed out with HTML to avoid relative pathing problems within URLs for href's.

Was it helpful?

Solution

The __FILE__ constant contains the file path, nothing else. Look at $_SERVER['REQUEST_URI'] and $_SERVER['HTTP_HOST'] to find out what URL was used to invoke the script. This is environment information that must be supplied by the web server that initiated the PHP script (e.g. Apache), it's not something PHP inherently knows.

OTHER TIPS

Am I wrong in thinking you could use:

$path = str_replace("/Users/user/Sites/MyWebsite", "localhost",$appPath);
echo($path);
//returns: localhost/application

Criticism welcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top