views:

1309

answers:

8

I'd like to find the base url of my application, so I can automatically reference other files in my application tree...

So given a file config.php in the base of my application, if a file in a subdirectory includes it, knows what to prefix a url with.

application/config.php
application/admin/something.php
application/css/style.css

So given that http://www.example.com/application/admin/something.php is accessed, I want it to be able to know that the css file is in $approot/css/style.css. In this case, $approot is "/application" but I'd like it to know if the application is installed elsewhere.

I'm not sure if it's possible, many applications (phpMyAdmin, Squirrelmail I think) have to set a config variable to begin with. It would be more user friendly if it just knew.

A: 

Unless you track this yourself, I don't believe this would have a definition. Or rather, you're asking PHP to track something that you're somewhat arbitrarily defining.

The long and short of it is, if I'm understanding your question correctly, I don't believe what you're asking for exists, at least not as "core" PHP; a given framework may provide a "root" URL relative to a directory structure that it understands.

Does that make sense?

theraccoonbear
+1  A: 

The REQUEST_URI combined with dirname() can tell you your current directory, relevant to the URL path:

<?php
  echo  dirname($_SERVER["REQUEST_URI"]);
?>

So http://example.com/test/test.php prints "/test" or http://example.com/ prints "/" which you can use for generating links to refer to other pages relative to the current path.

EDIT: just realized on re-reading that you might be asking about the on-disk path as opposed to the URL path. In that case, you want PHP's getcwd() function instead:

<?php
  echo  getcwd();
?>
Jay
+1  A: 

Put this in your config.php (which is in your app's root dir):

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

I think that'll do it.

Lucas Oman
That's close, but not quite. I think this gets messed up due to the aliasing of the server.
DGM
+1 for the interesting math though. :)
DGM
Ah, that's true. I guess whether this works depends on your server's config.
Lucas Oman
A: 

One solution would be to use relative paths for everything, that way it does not matter where the app is installed. For example, to get to your style sheet, use this:

../css/style.css
Adam Pierce
The problem is that this is in a template, but potentially called from different levels, so I need to know what level to call based on the script, or else I need the full url
DGM
A: 

If you want the path on the filesystem you can use $_SERVER['DOCUMENT_ROOT']
If you just want the path of the file that appears in the URL after the domain use $_SERVER['REQUEST_URI']

Unkwntech
A: 

I've never found a way to make it so.

I always end up setting a config variable with the server path to one level above web root. Then it's:

  • $configVar . 'public/whatever/' for stuff inside root,
  • but you can also include from outside with $configVar . 'phpInc/db.inc.php/' etc.
da5id
A: 

This works like a charm for me, anywhere I deploy, with or without rewrite rules:

$baseDir = 'http://'.$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']) != '/' ? dirname($_SERVER["SCRIPT_NAME"]).'/' : '/');

SchizoDuckie
This comes close, but it doesn't shave off subdirectories of the application from the url. So if I'm in http://hostname/application/subfolder/test.php and including a file in /application that defines this variable, it still has subfolder in the url. I want only /application
DGM
Try URL rewriting. Just rewrite everything to an index.php in /application/ (except some http resources ofcourse) Make that index.php forward it to the correct subfolder.
SchizoDuckie
Enable mod_rewrite in apache.htaccess in /application/RewriteEngine OnRewriteRule ^includes/ - [L] [OR] #do not apply to /includes OR RewriteRule ^images/ - [L] #do not apply to /imagesRewriteRule ^.* index.php #rewrite everything to index.phpParse $_SERVER['REQUEST_URI'] for your path!
SchizoDuckie
+2  A: 

I use the following in a homebrew framework... Put this in a file in the root folder of your application and simply include it.

define('ABSPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');

$tempPath1 = explode('/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));

for ($i = count($tempPath2); $i < count($tempPath1); $i++)
    array_pop ($tempPath3);

$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);

if ($urladdr{strlen($urladdr) - 1}== '/')
    define('URLADDR', 'http://' . $urladdr);
else
    define('URLADDR', 'http://' . $urladdr . '/');

unset($tempPath1, $tempPath2, $tempPath3, $urladdr);

The above code defines two constants. ABSPATH contains the absolute path to the root of the application (local file system) while URLADDR contains the fully qualified URL of the application. It does work in mod_rewrite situations.

Andrew Moore
Wow, that seems to work.
DGM
No it doesn’t. It returns the physical filesystem path and not the URL path.
Gumbo
ABSPATH returns the physical filesystem path, URLADDR returns the fully qualified URL of the application.
Andrew Moore
whoops, no, there's still something missing. I got ABSPATH=/path/to/real/application and URLADDR=http://hostname/ I guess I wanted URLADDR to be http://hostname/application/
DGM
Andrew, this was right, if I just remove one of the dirname() calls in the first line - the ABSPATH removed one too many directories. If you can edit the post to have only one dirname, I'll re-mark it as correct.
DGM
You are right, sorry. I have this code running in an includes/ folder up of my root application directory, hence the extra dirname(). Changed!
Andrew Moore
Works like a charm, Thanks!
Sijin