PHP readdir()関数
例
ディレクトリを開いて、その内容を読み取り、その後閉じます:
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
結果:
filename: cat.gif
filename: dog.gif
filename: horse.gif
filename: dog.gif
filename: horse.gif
定義と使い方
readdir() 関数は、ディレクトリ内の次のファイルのファイル名を返します。
構文
readdir(dir_handle);
| パラメータ | 説明 |
|---|---|
| dir_handle | オプション。以前に指定された、opendir()開かれたディレクトリハンドルリソース。このパラメータが指定されていない場合は、opendir() で開かれた最後のリンクが使用されます。 |
技術詳細
| 戻り値: | 成功した場合はファイル名を返し、失敗した場合は FALSE を返します。 |
|---|---|
| PHP バージョン: | 4.0+ |
PHP Directory リファレンスマニュアル その他の拡張機能