(PHP 4, PHP 5)
dirname — 返回路徑中的目錄部分
說明
string dirname ( string$path
)
給出一個(gè)包含有指向一個(gè)文件的全路徑的字符串,本函數(shù)返回去掉文件名后的目錄名。
參數(shù)
path
一個(gè)路徑。
在 Windows 中,斜線(/)和反斜線()都可以用作目錄分隔符。在其它環(huán)境下是斜線(/)。
返回值
返回 path 的父目錄。
如果在 path
中沒有斜線,則返回一個(gè)點(diǎn)('.'),表示當(dāng)前目錄。否則返回的是把
path
中結(jié)尾的
/component(最后一個(gè)斜線以及后面部分)去掉之后的字符串。
更新日志
版本 說明 5.0.0 dirname() 的操作從 PHP 5.0.0 版開始是二進(jìn)制安全的。 4.0.3 在這個(gè)版本中,dirname() 被修正為 POSIX 兼容。
范例
Example #1 dirname() 例子
<?php
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or on Windows)
echo "3) " . dirname("."); // 3) .
?>
注釋
Note:
dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".
Note:
dirname() is locale aware, so for it to see the correct directory name with multibyte character paths, the matching locale must be set using the setlocale() function.
Note:
Since PHP 4.3.0, you will often get a slash or a dot back from dirname() in situations where the older functionality would have given you the empty string.
檢查下面發(fā)生變化的例子:
<?php
// PHP 4.3.0 以前
dirname('c:/'); // 返回 '.'
// PHP 4.3.0 以后
dirname('c:/x'); // 返回 'c:'
dirname('c:/Temp/x'); // 返回 'c:/Temp'
dirname('/x'); // 返回 ''
?>
參見
basename() - 返回路徑中的文件名部分 pathinfo() - 返回文件路徑的信息 realpath() - 返回規(guī)范化的絕對(duì)路徑名