PHP scandir()関数
例
画像ディレクトリ内のファイルとディレクトリを一覧表示します。
<?php
$dir = "/images/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($a);
print_r($b);
?>
結果:
Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)
定義と使用法
scandir()関数は、指定されたディレクトリのファイルとディレクトリの配列を返します。
構文
scandir(directory, order, context)
パラメータ値
Parameter | Description |
---|---|
directory | Required. Specifies the directory to be scanned |
order | Optional. Specifies the sorting order. Default sort order is alphabetical in ascending order (0). Set to SCANDIR_SORT_DESCENDING or 1 to sort in alphabetical descending order, or SCANDIR_SORT_NONE to return the result unsorted |
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をスローします |
---|---|
PHPバージョン: | 5.0以降 |
PHP変更ログ: | PHP 5.4:順序定数が追加されました |
❮PHPディレクトリリファレンス