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);
}
}
?>
結果:
filename: cat.gif
filename: dog.gif
filename: horse.gif
定義と使用法
opendir()関数は、ディレクトリハンドルを開きます。
構文
opendir(path, context)
パラメータ値
Parameter | Description |
---|---|
path | Required. Specifies the directory path to be opened |
context | Optional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream |
技術的な詳細
戻り値: | 成功するとディレクトリハンドルリソースを返します。失敗するとFALSE。パスが有効なディレクトリでない場合、または権限制限またはファイルシステムエラーのためにディレクトリを開くことができない場合は、レベルE_WARNINGのエラーをスローします。関数名の前に「@」を追加すると、opendir()のエラー出力を非表示にできます。 |
---|---|
PHPバージョン: | 4.0以降 |
PHP変更ログ: | PHP 5.0:pathパラメーターがftp:// URLラッパーをサポートするようになりました |
❮PHPディレクトリリファレンス