Hàm scandir () trong PHP
Thí dụ
Liệt kê các tệp và thư mục bên trong thư mục hình ảnh:
<?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);
?>
Kết quả:
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] => .
)
Định nghĩa và Cách sử dụng
Hàm scandir () trả về một mảng các tệp và thư mục của thư mục được chỉ định.
Cú pháp
scandir(directory, order, context)
Giá trị tham số
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 |
Chi tiết kỹ thuật
Giá trị trả lại: | Một mảng các tệp và thư mục khi thành công, FALSE nếu thất bại. Ném E_WARNING nếu thư mục không phải là thư mục |
---|---|
Phiên bản PHP: | 5.0+ |
PHP Changelog: | PHP 5.4: Các hằng số thứ tự đã được thêm vào |
❮ Tham chiếu thư mục PHP