2010-08-07 121 views
30

的基本目录这是我的脚本的网址:localhost/do/index.php获取当前脚本

我想要一个变量或返回函数localhost/do(像$_SERVER['SERVER_NAME'].'/do'

+0

看来你真的不知道你想 – 2010-08-07 06:52:12

+0

我知道,但我的英语不好veryyyy伤心:( – Snoob 2010-08-07 09:45:50

回答

26

尝试:

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

编辑:

$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; 

这应该会返回localhost/do/

+0

不作品...; n – Snoob 2010-08-07 05:43:48

+0

我想我知道你在看什么 - 查看我的更新 – Calvin 2010-08-07 05:57:12

+1

使用'dirname()'就足够了,它不检查目录是否真的存在。从dirname('/ solar/system')返回的值是''/ solar'',无论目录/太阳能存在与否。 – kiamlaluno 2010-08-07 06:05:40

8

PHP有字符串解析许多功能可以通过简单的一个在线进行片段
目录名()(你问)和parse_url()(你需要)是其中之一

<?php 

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

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

echo "Current dir is: ".$curdir; 
echo "<br>"; 
在浏览器

地址栏是

http://localhost/do/index.php 

输出是

Request uri is: /do/index.php 
Current dir is: /do/ 
+0

你可以举个例子,它不适用 – Snoob 2010-08-07 09:39:43

+0

@Snoob这里是你的例子。如果你能够解释你想要使用那个“基本词典”,我会告诉你为什么它不起作用 – 2010-08-07 09:58:00

+1

如果url是localhost/do/index.php,但是当我使用localhost/do。输出:Request uri是:/ ds/Current dir is:\/ – Snoob 2010-08-07 11:04:36

1

dirname会给你一个文件路径的目录部分。例如:

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

获取当前脚本的URL是有点麻烦,但$_SERVER['REQUEST_URI']将域名后恢复你的部分(即,它会给你“/do/index.php”)。

+0

echo dirname($ _ SERVER ['REQUEST_URI']); http:// localhost/do/=> return \; http:// localhost/do/index.php => return/do – Snoob 2010-08-07 05:51:39

+0

要注意,如果URL是'http:// localhost/do/index.php','localhost'不包括从'dirname($ _ SERVER ['REQUEST_URI'])''返回的值。 – kiamlaluno 2010-08-07 06:07:42

+0

@Snoob和@kiamlaluno:我已经在我原来的帖子中说过这个,'$ _SERVER ['REQUEST_URI']'会返回域名后面的部分(或者在这个例子中是服务器名称)。 – hbw 2010-08-07 06:15:26

2

如果要包括服务器名称,我的理解,然后将下面的代码片段应该做你所要求的:

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

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

$result = $_SERVER['SERVER_NAME'] . '/' . dirname($_SERVER['REQUEST_URI']); 
30
$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']); 
+0

优秀的答案,但这不适用于IIS,因为dirname返回windows反斜杠 – icc97 2013-04-28 09:03:53

+3

实际上,如果请求URI是localhost/do /而不是localhost /做/ index.php文件。我通过使用总是包含索引的PHP_SELF来解决这个问题。PHP,所以'$ current_dir_url = $ _SERVER ['SERVER_NAME']。 dirname($ _ SERVER ['PHP_SELF']);' – icc97 2013-04-28 09:21:31

+0

我不建议使用dirname()。起初:这个答案是错误的,因为它返回的是父目录,而不是当前目录,但即使添加了尾部斜线(就像@Your Common Sense所做的那样),您正面临$ _SERVER ['REQUEST_URI']的问题,因为它只有在调用'http:// example.com /'时才包含'/'。通过'dirname()'会返回'/',并且当你添加第二个'/'时''会得到'//'。你使用'$ _SERVER ['PHP_SELF'])''也有同样的问题。查看我的答案获取更多信息。 – mgutt 2015-03-19 15:06:27

6

当我实施一些这些问题的答案,我打的我在使用IIS时遇到了一些问题,我也希望在协议中使用完全限定的URL。我使用PHP_SELF而不是REQUEST_URI作为dirname('/do/')在Windows中给出'/'(或'\'),当您想要返回'/do/'时。

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') { 
    $protocol = 'http://'; 
} else { 
    $protocol = 'https://'; 
} 
$base_url = $protocol . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']); 
8

我建议不要使用dirname()。我有几个问题,多个斜线和意想不到的结果。这就是为什么我创建currentdir()的原因:

function currentdir($url) { 
    // note: anything without a scheme ("example.com", "example.com:80/", etc.) is a folder 
    // remove query (protection against "?url=http://example.com/") 
    if ($first_query = strpos($url, '?')) $url = substr($url, 0, $first_query); 
    // remove fragment (protection against "#http://example.com/") 
    if ($first_fragment = strpos($url, '#')) $url = substr($url, 0, $first_fragment); 
    // folder only 
    $last_slash = strrpos($url, '/'); 
    if (!$last_slash) { 
     return '/'; 
    } 
    // add ending slash to "http://example.com" 
    if (($first_colon = strpos($url, '://')) !== false && $first_colon + 2 == $last_slash) { 
     return $url . '/'; 
    } 
    return substr($url, 0, $last_slash + 1); 
} 

为什么你不应该使用目录名()

假设你有image.jpg位于images/,你有下面的代码:

<img src="<?php echo $url; ?>../image.jpg" /> 

现在假定$url可以包含不同的值:

  • http://example.com/index.php
  • http://example.com/images/
  • http://example.com/images//
  • http://example.com/

不管它包含,我们需要在当前目录下产生一个工作深层链接。您尝试dirname(),面临以下问题:

1)不同的结果文件和目录

文件
dirname('http://example.com/images/index.php')回报http://example.com/images

目录
dirname('http://example.com/images/')回报http://example.com

但没问题。我们可以通过一招涵盖这样的:
dirname('http://example.com/images/' . '&') . '/'回报http://example.com/images/

现在dirname()回报在这两种情况下所需要的当前目录。但是,我们将有其他问题:

2)有些多个斜杠将被删除
dirname('http://example.com//images//index.php')返回http://example.com//images

当然这个URL没有很好形成,但多个斜线发生,我们需要像浏览器作为网站管理员使用它们来验证他们的输出。也许你想知道,但下面例子的前三张图片全部加载完毕。

<img src="http://example.com/images//../image.jpg" /> 
<img src="http://example.com/images//image.jpg" /> 
<img src="http://example.com/images/image.jpg" /> 
<img src="http://example.com/images/../image.jpg" /> 

这就是为什么你应该保持多个斜杠的原因。由于dirname()只删除了一些斜线,我打开了bug ticket

3。)根URL不返回根目录
dirname('http://example.com')回报http:
dirname('http://example.com/')回报http:

4)根目录返回相对路径
dirname('foo/bar')回报.

我希望/

5)错误的URL编码
dirname('foo/bar?url=http://example.com')回报foo/bar?url=http:

所有测试结果:
http://www.programmierer-forum.de/aktuelles-verzeichnis-alternative-zu-dirname-t350590.htm#4329444

+0

使你认为dirname不是用于网址 – icc97 2015-08-03 20:47:03

+0

这应该是被接受的答案 – Luvias 2018-02-09 15:42:11

1

最好的办法是使用爆炸/破灭功能(内置PHP)像所以

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$parts = explode('/',$actual_link); 
$parts[count($parts) - 1] = ""; 
$actual_link = implode('/',$parts); 
echo $actual_link; 
0

我的建议:

const DELIMITER_URL = '/'; 
$urlTop = explode(DELIMITER_URL, trim(input_filter(INPUT_SERVER,'REQUEST_URI'), DELIMITER_URL))[0] 

测试:

const DELIMITER_URL = '/'; 
$testURL = "/top-dir"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

$testURL = "/top-dir/"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

$testURL = "/top-dir/test"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

$testURL = "/top-dir/test/"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

$testURL = "/top-dir/test/this.html"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

$testURL = "/top-dir/test.html"; 
var_dump(explode(DELIMITER_URL, trim($testURL, DELIMITER_URL))[0]); 

测试输出:

string(7) "top-dir" 
string(7) "top-dir" 
string(7) "top-dir" 
string(7) "top-dir" 
string(7) "top-dir" 
string(7) "top-dir"