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
2010-08-07 05:42:54
not works...;.n
Snoob
2010-08-07 05:43:48
I think I know what you're getting at -- see my update
Calvin L
2010-08-07 05:57:12
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
2010-08-07 06:05:40
that's ugliest way I've ever seen. you've at least use implode
Col. Shrapnel
2010-08-07 06:51:17
What is the nice way?
Snoob
2010-08-07 07:55:51
answered 1000 times already. dirname()
Col. Shrapnel
2010-08-07 08:03:09
But not works :( can you give a example ?
Snoob
2010-08-07 09:37:58
+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
2010-08-07 05:46:19
@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
2010-08-07 09:58:00
It works if url is localhost/do/index.php, but when i use localhost/do. Output: Request uri is: /ds/ Current dir is: \/
Snoob
2010-08-07 11:04:36
+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
2010-08-07 05:47:27
echo dirname($_SERVER['REQUEST_URI']); http://localhost/do/ => return \; http://localhost/do/index.php => return /do
Snoob
2010-08-07 05:51:39
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
2010-08-07 06:07:42
@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
2010-08-07 06:15:26
+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
2010-08-07 05:56:26