ฟังก์ชั่น readdir()
ความหมายของฟังก์ชั่นและการใช้งาน
การอ่านรายการที่อยู่ในไดเร็กทอรี โดยจะคืนค่าชื่อไฟล์หรือชื่อโฟลเดอร์ ณ ตำแหน่งที่ Pointer ชี้อยู่ และเลื่อน pointer ไปยังตำแหน่งถัดไปด้วย ด้วยฟังก์ชั่น readdir()
รูปแบบการเขียน (Syntax)
string readdir ([ resource $dir_handle ] )
ตัวอย่างที่ 1
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
ตัวอย่างที่ 2
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>