函式名稱:DirectoryIterator::getFilename()
適用版本:PHP 5, PHP 7
函式描述:該函式用於獲取目錄迭代器中的當前檔名。
用法:
$dir = new DirectoryIterator('/path/to/directory');
foreach ($dir as $file) {
if (!$file->isDot()) {
echo $file->getFilename() . "<br/>";
}
}
引數說明:該函式沒有任何引數。
返回值:返回一個字串,表示當前目錄迭代器中的檔名。
示例:
假設目錄 /path/to/directory
包含以下檔案:
- file1.txt
- file2.pdf
- file3.png
$dir = new DirectoryIterator('/path/to/directory');
foreach ($dir as $file) {
if (!$file->isDot()) {
echo $file->getFilename() . "<br/>";
}
}
輸出結果:
file1.txt
file2.pdf
file3.png
注意事項:
- 在使用
DirectoryIterator
之前,務必將path/to/directory
替換為你要操作的實際目錄路徑。 - 推薦使用
isDot()
方法來排除目錄中的.
和..
特殊檔案。 - 如果目錄中沒有可迭代的檔案,該函式將返回空字串。