update page now
Laravel Live Japan

rewinddir

(PHP 4, PHP 5, PHP 7, PHP 8)

rewinddirディレクトリハンドルを元に戻す

説明

rewinddir(?resource $dir_handle = null): void

dir_handle で指定されたディレクトリの ストリームをディレクトリの先頭にリセットします。

パラメータ

dir_handle
opendir() が事前にオープンした ディレクトリハンドルを示す resourcedir_handlenull の場合は、 opendir() が最後にオープンしたものを使用します。

戻り値

値を返しません。

変更履歴

バージョン 説明
8.5.0 dir_handlenull を指定することは、推奨されなくなりました。 代わりに、最後にオープンしたディレクトリハンドルを明示的に指定すべきです。
8.0.0 dir_handle は、nullable になりました。

完全なサンプルコードは、 opendir() のドキュメントを参照ください。

参考

  • opendir() - ディレクトリハンドルをオープンする
  • readdir() - ディレクトリハンドルからエントリを読み込む
  • closedir() - ディレクトリハンドルをクローズする
  • dir() - ディレクトリクラスのインスタンスを返す
  • is_dir() - ファイルがディレクトリかどうかを調べる
  • glob() - パターンにマッチするパス名を探す
  • scandir() - 指定されたパスのファイルとディレクトリのリストを取得する
add a note

User Contributed Notes 2 notes

up
9
osamahussain897 at gmail dot com
7 years ago
/* Source Code */

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    // List files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    rewinddir();
    // List once again files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>

/* Result */

filename: cat.gif
filename: dog.gif
filename: horse.gif
filename: cat.gif
filename: dog.gif
filename: horse.gif
up
4
ASchmidt at Anamera dot net
7 years ago
It is crucial to note that rewinddir() does not simply start over at the beginning of the SAME directory list. Instead, this function first re-reads the directory - thus any file that were deleted (or inserted) since the original opendir() will be reflected after "rewinding".

In that respect, rewinddir() is equivalent to a closedir(), opendir() sequence, but without obtaining a new handle.
To Top