的基本目錄這是我的腳本的網址:localhost/do/index.php
獲取當前腳本
我想要一個變量或返回函數localhost/do
(像$_SERVER['SERVER_NAME'].'/do'
)
的基本目錄這是我的腳本的網址:localhost/do/index.php
獲取當前腳本
我想要一個變量或返回函數localhost/do
(像$_SERVER['SERVER_NAME'].'/do'
)
嘗試:
$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/
不作品...; n – Snoob 2010-08-07 05:43:48
我想我知道你在看什麼 - 查看我的更新 – Calvin 2010-08-07 05:57:12
使用'dirname()'就足夠了,它不檢查目錄是否真的存在。從dirname('/ solar/system')返回的值是''/ solar'',無論目錄/太陽能存在與否。 – kiamlaluno 2010-08-07 06:05:40
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/
dirname
會給你一個文件路徑的目錄部分。例如:
echo dirname('/path/to/file.txt'); // Outputs "/path/to"
獲取當前腳本的URL是有點麻煩,但$_SERVER['REQUEST_URI']
將域名後恢復你的部分(即,它會給你「/do/index.php」)。
echo dirname($ _ SERVER ['REQUEST_URI']); http:// localhost/do/=> return \; http:// localhost/do/index.php => return/do – Snoob 2010-08-07 05:51:39
要注意,如果URL是'http:// localhost/do/index.php','localhost'不包括從'dirname($ _ SERVER ['REQUEST_URI'])''返回的值。 – kiamlaluno 2010-08-07 06:07:42
@Snoob和@kiamlaluno:我已經在我原來的帖子中說過這個,'$ _SERVER ['REQUEST_URI']'會返回域名後面的部分(或者在這個例子中是服務器名稱)。 – hbw 2010-08-07 06:15:26
如果要包括服務器名稱,我的理解,然後將下面的代碼片段應該做你所要求的:
$result = $_SERVER['SERVER_NAME'] . dirname(__FILE__);
$result = $_SERVER['SERVER_NAME'] . __DIR__; // PHP 5.3
$result = $_SERVER['SERVER_NAME'] . '/' . dirname($_SERVER['REQUEST_URI']);
$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);
優秀的答案,但這不適用於IIS,因爲dirname返回windows反斜槓 – icc97 2013-04-28 09:03:53
實際上,如果請求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
我不建議使用dirname()。起初:這個答案是錯誤的,因爲它返回的是父目錄,而不是當前目錄,但即使添加了尾部斜線(就像@Your Common Sense所做的那樣),您正面臨$ _SERVER ['REQUEST_URI']的問題,因爲它只有在調用'http:// example.com /'時才包含'/'。通過'dirname()'會返回'/',並且當你添加第二個'/'時''會得到'//'。你使用'$ _SERVER ['PHP_SELF'])''也有同樣的問題。查看我的答案獲取更多信息。 – mgutt 2015-03-19 15:06:27
當我實施一些這些問題的答案,我打的我在使用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']);
我建議不要使用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
最好的辦法是使用爆炸/破滅功能(內置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;
我的建議:
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"
看來你真的不知道你想 – 2010-08-07 06:52:12
我知道,但我的英語不好veryyyy傷心:( – Snoob 2010-08-07 09:45:50