1 / 19

การใช้ PHP ติดต่อกับ Text File

การใช้ PHP ติดต่อกับ Text File. เสรี ชิโนดม seree@buu.ac.th. การเปิดไฟล์. ใช้คำสั่ง fopen มีรูปแบบดังนี้ int fopen(“ พาธและชื่อไฟล์ ”, “ โหมดการเปิดไฟล์ ”)

paige
Télécharger la présentation

การใช้ PHP ติดต่อกับ Text File

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. การใช้ PHP ติดต่อกับText File เสรี ชิโนดม seree@buu.ac.th

  2. การเปิดไฟล์ • ใช้คำสั่ง fopen มีรูปแบบดังนี้ int fopen(“พาธและชื่อไฟล์”, “โหมดการเปิดไฟล์”) • file จะถูกเปิดจาก file system และจะมีการ return file pointer กลับมา หากไม่สามารถเปิด file ได้ function จะ return เท็จ • mode มีดังนี้ • 'r' - เปิดเพื่ออ่านอย่างเดียว, file pointer ชี้ไปยังตำแหน่งเริ่มต้นของ file • 'r+' - เปิดเพื่ออ่านและเขียน, file pointer ชี้ไปยังตำแหน่งเริ่มต้นของ file PHP Programming

  3. การเปิดไฟล์ • 'w' - เปิดเพื่อเขียนอย่างเดียว, file pointer ชี้ไปยังตำแหน่งเริ่มต้นของ file และทำการลบข้อมูลใน file ทั้งหมด หาก file ไม่มีอยู่จริง จะพยายามสร้าง file ใหม่ขึ้นมา • 'w+'- เปิดเพื่ออ่านและเขียน, file pointer ชี้ไปยังตำแหน่งเริ่มต้นของ file และทำการลบข้อมูลใน file ทั้งหมด หาก file ไม่มีอยู่จริง จะพยายามสร้าง file ใหม่ขึ้นมา • 'a' - เปิดเพื่อเขียนอย่างเดียว, file pointer ชี้ไปยังตำแหน่งสุดท้ายของ file (EOF:end of file) หาก file ไม่มีอยู่จริง จะพยายามสร้าง file ใหม่ขึ้นมา • 'a+' - เปิดเพื่ออ่านและเขียน, file pointer ชี้ไปยังตำแหน่งสุดท้ายของ file (EOF:end of file) หาก file ใหม่ขึ้นมา PHP Programming

  4. ตัวอย่าง $FILE = fopen(“config.inf”, “r”); //ตรวจสอบถ้ามีข้อผิดพลาด if (!$FILE) { echo “<H1> ERROR, file not found </H1>” ; } else { print (“File Found”); } PHP Programming

  5. การปิดไฟล์ • ใช้คำสั่ง fclose ปิด file pointer ที่เปิดอยู่ ซึ่งจะ return ค่าเป็นจริงหากสามารถเปิด file pointer ได้มีรูปแบบคือ int fclose(int fp) • ตัวอย่าง $FILE = fopen(“config.inf”, “r”); ….. fclose($FILE); PHP Programming

  6. การอ่านข้อมูลจากไฟล์ทีละข้อความการอ่านข้อมูลจากไฟล์ทีละข้อความ • มีฟังก์ชันที่ใช้ดังนี้ string fread (ตัวแปร์ไฟล์, ความยาวที่จะอ่านเข้ามา) string fgets (ตัวแปร์ไฟล์, ความยาวที่จะอ่านเข้ามา) string fgetss (ตัวแปร์ไฟล์, ความยาวที่จะอ่านเข้ามา) • รุปแบบการใช้ทั้ง 3 ฟังก์ชันจะคล้ายกัน PHP Programming

  7. ตัวอย่างไฟล์ fread1.php <? $FILE = fopen( "info.txt" , "r" ); $text =fread($FILE, 20 ); print (“$text <BR>”); fclose( $FILE ); ?> PHP Programming

  8. การใช้ fgets, fgetss • fgets จะอ่านไปจนถึงท้ายบรรทัดจึงถือว่าเป็นการอ่าน 1 ครั้ง <? $FILE = fopen( "info.txt" , "r" ); echo “Each time read 20 characters <P>” ; $text =fgets($FILE, 20 ); print (“data = $text <BR>”); fclose( $FILE ); ?> PHP Programming

  9. การใช้ fgets, fgetss • fgetss ทำงานเหมือน fgets แต่จะตัดแท็ก HTML ออกไป <? $FILE = fopen( ”bookmark.htm" , "r" ); while (!feof($FILE) { $text =fgetss($FILE, 255 ); print (“$text <BR>”) ; } fclose( $FILE ); ?> PHP Programming

  10. การอ่านข้อมูลจากไฟล์ทีละตัวอักษรการอ่านข้อมูลจากไฟล์ทีละตัวอักษร • ใช้ฟังก์ชัน fgetc • ตัวอย่าง <? $FILE = fopen( "info.txt" , "r" ); $char = fgetc($FILE); print (“Character is $char”); fclose( $FILE ); ?> PHP Programming

  11. การอ่านข้อมูลจากไฟล์ทีละตัวอักษรทั้งไฟล์การอ่านข้อมูลจากไฟล์ทีละตัวอักษรทั้งไฟล์ <? $FILE = fopen( "info.txt" , "r" ); while (!feof($FILE) { $char = fgetc($FILE); print (“$char”) ; } fclose( $FILE ); ?> หมายเหตุ ฟังก์ชัน feof() ใช้ตรวจสอบว่าสิ้นสุดไฟล์หรือยัง PHP Programming

  12. ตัวอย่างการประยุกต์ • ตรวจสอบว่ามีอักษร a กี่ตัวในไฟล์ <? $FILE = fopen( "info.txt" , "r" ); while (!feof($FILE) { print (" $char ") ; $char = fgetc($FILE); if(($char == "a" ||($char == "A" )){ $a++ } } fclose( $FILE ); print "<BR><BR> Have ‘a’ = $a"; ?> PHP Programming

  13. การเขียนไฟล์ • int fwrite(ตัวแปรไฟล์,ข้อมูลที่ต้องการเขียน,ความยาว) • int fputs(ตัวแปรไฟล์,ข้อมูลที่ต้องการเขียน,ความยาว) • ตัวอย่าง <? $FILE = fopen("myinfo" , "w" ); fputs( $FILE, "My name is Oh"); echo "write file" ; fclose( $FILE ); ?> PHP Programming

  14. การเขียนไฟล์ • $fname =“somsak” ; • $lname=“sansook”; • fputs($FILE, “My name is $fname.\r\n”); • fputs($FILE, “My name is $lname.\r\n”); PHP Programming

  15. ตัวอย่างการเขียนข้อมูลในอะเรย์ลงไฟล์ตัวอย่างการเขียนข้อมูลในอะเรย์ลงไฟล์ <? $data = file("info.txt");//อ่านข้อมูลจากไฟล์info.txt $FILE = fopen( "info.htm" , "w" ); fputs( $FILE , "<html><body>" ); fputs( $FILE , "<font color=blue>" ); for ( $i=0; $i<count($data) ; $i++ ) { fputs( $FILE , $data[$i] . "<br>" ); } fputs( $FILE , "</html></body>" ); fclose( $FILE ); echo "write info.htm in HTML format"; ?> PHP Programming

  16. ฟังก์ชัน rewind • ใช้ย้ายfile pointer กลับมาต้นไฟล์ <? $FILE = fopen( "info.txt" , "r" ); $text =fgets($FILE, 50); print "text is $text <BR>"; rewind( $FILE ); $text =fgets($FILE, 50); print "Again text is $text <BR>"; fclose( $FILE ); ?> PHP Programming

  17. ฟังก์ชัน fseek • ใช้ย้ายfile pointer กลับไปยังตำแหน่งที่ต้องการ เช่นต้องการอ่านอักขระตัวที่ 101-150 จากไฟล์info.txt <? $FILE = fopen( "info.txt" , "r" ); fseek($FILE, 101); $text =fgets($FILE, 50); print "text is <U>$text </U>"; fclose ($FILE); ?> PHP Programming

  18. ฟังก์ชัน ftell • ใช้บอกตำแหน่งของ file pointer ว่าอยู่ในตำแหน่งใด <? $FILE = fopen( "info.txt" , "r" ); $pos = ftell($FILE); print "Position is <U>$pos </U>"; fclose(($FILE); ?> PHP Programming

  19. การตรวจสอบไฟล์ • file_exits ใช้ในการตรวจสอบว่ามีไฟล์อยู่หรือไม่ • file_size ใช้ในการตรวจสอบขนาดของไฟล์ <? $f = "info.txt” ; if ( file_exits( $f ) ) { print “Found $f” ; } else { print “Not found $f” ; ?> PHP Programming

More Related