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