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: ตอนนี้พารามิเตอร์พาธรองรับftp:// URL wrapper |
❮ การอ้างอิงไดเรกทอรี PHP