DirectoryIterator::getBasename()函数是PHP中的内置函数,用于获取当前DirectoryIterator项的基本名称。
用法:
string DirectoryIterator::getBasename( string $suffix )
参数:此函数接受单个参数$suffix,该参数保存基本名称以后缀结尾。
返回值:此函数返回当前DirectoryIterator项的基本名称。
以下示例程序旨在说明PHP中的DirectoryIterator::getBasename()函数:
程序1:
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
// Loop runs while directory is valid
while ($directory->valid()) {
// Display the base name
echo $directory->getBasename() . "
";
// Move to the next element
$directory->next();
}
?>
输出:
.
..
applications.html
bitnami.css
dashboard
favicon.ico
geeks.PNG
gfg.php
img
index.php
webalizer
xampp
程序2:
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
// Loop runs for each element of directory
foreach($directory as $dir) {
// Display the key and basename
echo $dir->key() . " => " .
$dir->getBasename() . "
";
}
?>
输出:
0 => .
1 => ..
2 => applications.html
3 => bitnami.css
4 => dashboard
5 => favicon.ico
6 => geeks.PNG
7 => gfg.php
8 => img
9 => index.php
10 => webalizer
11 => xampp
注意:此功能的输出取决于服务器文件夹的内容。