PHP opendir()関数
例
ディレクトリを開き、その内容を読み取って閉じます:
<?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
定義と用法
opendir() 関数はディレクトリハンドルを開きます。
構文
opendir(path,context);
| パラメータ | 説明 |
|---|---|
| path | 必須。開くディレクトリのパスを指定します。 |
| context | 省略可能。ディレクトリハンドルのコンテキストを指定します。context は、ディレクトリストリームの動作を変更できる一連のオプションです。 |
技術詳細
| 戻り値: | 成功した場合はディレクトリハンドルリソースを返します。失敗した場合は FALSE を返します。パスが有効なディレクトリでない場合、または権限制限やファイルシステムエラーによりディレクトリを開けない場合は、E_WARNING レベルのエラーが発生します。関数名の前に「@」を追加することで、opendir() のエラー出力を非表示にできます。 |
|---|---|
| PHP バージョン: | 4.0+ |
| PHP 変更履歴: | PHP 5.0:path パラメータでサポートftp://URL ラッパープロトコル。 |
PHP Directory リファレンスマニュアル その他の拡張