วันอังคารที่ 7 เมษายน พ.ศ. 2552

จัดการ Directory ใน PHP

php สามารถอ่านไดเรคทอรี่ Directory ว่ามีไฟล์อะไรบ้าง และสามารถลบแ้ก้ไขไฟล์นั้นได้ด้วย คำสั่งที่ใช้ในการอ่านไฟล์ใน Directory ประกอบด้วย
- open_dir เปิด Directory
- read_dir อ่าน Directory ที่ open_dir ว่ามีไฟล์อะไรบ้าง
- close_dir ปิด open_dir เพื่อคืนหน่่วยความจำให้ระบบ

ตัวอย่างการใช้งาน

// Note that !== did not exist until 4.0.0-RC2

if ($handle = opendir('/path/to/files')) { // path ที่ต้องการอ่านไฟล์ในที่จะอ่านในโฟรเดร์ files
echo
"Directory handle: $handle\n";
echo
"Files:\n";

/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo
"$file\n";
}

/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo
"$file\n";
}

closedir($handle);
}
?>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

ท่านสามารถสอบถามปัญหาเกี่ยวกับ php html javascript flash ฯลฯ เกี่ยวกับการทำเว็บได้ครับ ผ่าน coment ได้ครับ ยินดีตอบทุกปัญหา