tags:

views:

64

answers:

5

This is current script url : localhost/do/index.php -> I want a var or function that return localhost/do :) (something like $_SERVER['SERVER_NAME'].'/do');

A: 

Try:

$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
print_r($parts);

EDIT:

$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
 $dir .= $parts[$i] . "/";
}
echo $dir;

This should return localhost/do/

Calvin L
not works...;.n
Snoob
I think I know what you're getting at -- see my update
Calvin L
It is enough to use `dirname()`, which doesn't check if the directory really exists. The value returned from `dirname('/solar/system'`) is `'/solar'`, whatever the directory /solar exists, or not.
kiamlaluno
that's ugliest way I've ever seen. you've at least use implode
Col. Shrapnel
What is the nice way?
Snoob
answered 1000 times already. dirname()
Col. Shrapnel
But not works :( can you give a example ?
Snoob
+1  A: 

php has many functions for string parsing which can be done with simple one-line snippets
dirname() (which you asked for) and parse_url() (which you need) are among them

<?php

echo "Request uri is: ".$_SERVER['REQUEST_URI'];
echo "<br>";

$curdir = dirname($_SERVER['REQUEST_URI'])."/";

echo "Current dir is: ".$curdir;
echo "<br>";

address bar in browser is

http://localhost/do/index.php

output is

Request uri is: /do/index.php
Current dir is: /do/
Col. Shrapnel
can you give a example, it not works
Snoob
@Snoob here goes your example. Now if you'll be able to explain what you're trying to use that "base dic" for, I'll tell you why it's not working
Col. Shrapnel
It works if url is localhost/do/index.php, but when i use localhost/do. Output: Request uri is: /ds/ Current dir is: \/
Snoob
+1  A: 

dirname will give you the directory portion of a file path. For example:

echo dirname('/path/to/file.txt');  // Outputs "/path/to"

Getting the URL of the current script is a little trickier, but $_SERVER['REQUEST_URI'] will return you the portion after the domain name (i.e. it would give you "/do/index.php").

htw
echo dirname($_SERVER['REQUEST_URI']); http://localhost/do/ => return \; http://localhost/do/index.php => return /do
Snoob
To notice that if the URL is `http://localhost/do/index.php`, `localhost` is not included from the value returned from `dirname($_SERVER['REQUEST_URI'])`.
kiamlaluno
@Snoob and @kiamlaluno: I already addressed this in my original post by saying `$_SERVER['REQUEST_URI']` will return you the portion after the domain name (or, in this case, the server name).
htw
Are you tried run index.php use localhost/do
Snoob
+1  A: 

If you want to include the server name, as I understood, then the following code snippets should do what you are asking for:

$result = $_SERVER['SERVER_NAME'] . dirname(__FILE__);

$result = $_SERVER['SERVER_NAME'] . __DIR__; // PHP 5.3

$result = $_SERVER['SERVER_NAME'] . '/' . dirname($_SERVER['REQUEST_URI']);
kiamlaluno
+1  A: 

$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);

www333