1 / 17

File & Directory Management การจัดการไฟล์และ ไดเรคทอ รี

File & Directory Management การจัดการไฟล์และ ไดเรคทอ รี. Content. 1. Open/Write/Close File Functions 2. Read Data Functions 3. Other File Operations 4. Directory Functions 5. Other Directory Operations. Open/Write/Close a File. fopen () เพื่อใช้ในการเปิดไฟล์ .

Télécharger la présentation

File & Directory Management การจัดการไฟล์และ ไดเรคทอ รี

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File & Directory Managementการจัดการไฟล์และไดเรคทอรี

  2. Content 1. Open/Write/Close File Functions 2. Read Data Functions 3. Other File Operations 4. Directory Functions 5. Other Directory Operations

  3. Open/Write/Close a File • fopen() เพื่อใช้ในการเปิดไฟล์. • fwrite() เพื่อใช้ในการเขียนไฟล์ • fclose() เพื่อใช้ในการปิดไฟล์ • modeที่ใช้ในการเปิดไฟล์. • r อ่านอย่างเดียว • w สร้างไฟล์โดยถ้ามีไฟล์เดิมอยู่แล้วจะทำการลบทิ้งและสร้างไฟล์ขึ้นมาใหม่ • a กรณีที่มีไฟล์อยู่แล้วจะทำการเขียนไฟล์ต่อจากที่มีอยู่

  4. File Open/Close Syntax Syntax fopen(filename, mode); fwrite(filename,text); fclose(object);

  5. File Open/Close Example • <?php • $toread = fopen('text.txt','r'); • while (!feof($toread)) { • echo fgets($toread, 1024); • echo '<br />'; • } • fclose($toread); • ?>

  6. File Write Example <?php $towrite= fopen('text.txt','w'); fwrite($towrite,"PHP Write File Line1\r\n"); fwrite($towrite,"PHP Write File Line2\r\n"); fclose($towrite); ?>

  7. File Write Example <?php $towrite= fopen('text.txt',‘a'); fwrite($towrite,"PHP Write File Line1\r\n"); fwrite($towrite,"PHP Write File Line2\r\n"); fclose($towrite); ?>

  8. Read Data • There are two main functions to read data: • fgets($handle,$bytes) • Reads up to $bytes of data, stops at newline or end of file (EOF) • fread($handle,$bytes) • Reads up to $bytes of data, stops at EOF.

  9. Read Data • We need to be aware of the End Of File (EOF) point.. • feof($handle) • Whether the file has reached the EOF point. Returns true if have reached EOF.

  10. Read Data • We need to be aware of the End Of File (EOF) point.. • feof($handle) • Whether the file has reached the EOF point. Returns true if have reached EOF.

  11. File Open/Close Example • <?php • $toread = fopen('text.txt','r'); • while (!feof($toread)) { • echo fgets($toread, 1024); • echo '<br />'; • } • fclose($toread); • ?>

  12. Other File Operations • Delete file • unlink('filename'); • Rename (file or directory) • rename('old name', 'new name'); • Copy file • copy('source', 'destination'); • And many, many more! www.php.net/manual/en/ref.filesystem.php

  13. Directories • Open a directory • $handle = opendir('dirname'); • $handle 'points' to the directory • Read contents of directory • readdir($handle) • Returns name of next file in directory • Files are sorted as on file system • Close a directory • closedir($handle) • Closes directory 'stream'

  14. Directory Example <?php $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo"$file <br />"; } closedir($handle); ?>

  15. Other Directory Operations • Get current directory • getcwd() • Change Directory • chdir('dirname'); • Create directory • mkdir('dirname'); • Delete directory (MUST be empty) • rmdir('dirname'); • And more! • www.php.net/manual/en/ref.dir.php

  16. แบบฝึกหัด • จงเขียนโปรแกรมสร้าง Directory และบันทึกไฟล์ .txt • รายละเอียดมีดังนี้ • รหัสชื่อ นามสกุล • ที่อยู่ • ที่ทำงาน

  17. Q & A

More Related