View Full Version : Get base URL

11-05-2007, 09:35 PM

I want to get the actual base directory in my PHP script. Now I use the following code to get the actual location:


In case of http://mysite/test/index.php the result is mysite/test/index.php and in case of http://mysite/test the result is mysite/test.

I want to get only the directory path (mysite/test) without filename in both cases.

I have tried pathinfo(), dirname() but both removes always the last tag independent if it is a file or dir. So in case of http://mysite/test I got only mysite instead of mysite/test.

Any idea how to solve this?

11-06-2007, 09:07 AM
Try to use this code

//return absolute path
$dir = dirname(__FILE__);

You could also replace absolute path with HTTP_HOST if it is necessary for your purpose with str_replace