ไฟล์ PHP เปิด/อ่าน/ปิด
ในบทนี้เราจะสอนวิธีการเปิด อ่าน และปิดไฟล์บนเซิร์ฟเวอร์
PHP เปิดไฟล์ - fopen()
วิธีที่ดีกว่าในการเปิดไฟล์คือการใช้fopen()
ฟังก์ชัน ฟังก์ชันนี้มีตัวเลือกให้คุณมากกว่าreadfile()
ฟังก์ชัน
เราจะใช้ไฟล์ข้อความ "webdictionary.txt" ระหว่างบทเรียน:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
พารามิเตอร์แรกของfopen()
มีชื่อของไฟล์ที่จะเปิด และพารามิเตอร์ที่สองระบุว่าควรเปิดไฟล์ในโหมดใด ตัวอย่างต่อไปนี้ยังสร้างข้อความหากฟังก์ชัน fopen() ไม่สามารถเปิดไฟล์ที่ระบุได้:
ตัวอย่าง
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
เคล็ดลับ:fread()
และฟังก์ชันfclose()
ต่างๆ จะอธิบายไว้ด้านล่าง
ไฟล์สามารถเปิดได้ในโหมดใดโหมดหนึ่งต่อไปนี้:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP อ่านไฟล์ - fread()
ฟัง ก์fread()
ชั่นอ่านจากไฟล์ที่เปิดอยู่
พารามิเตอร์แรกของfread()
ประกอบด้วยชื่อของไฟล์ที่จะอ่าน และพารามิเตอร์ที่สองระบุจำนวนไบต์สูงสุดที่จะอ่าน
โค้ด PHP ต่อไปนี้อ่านไฟล์ "webdictionary.txt" ต่อท้าย:
fread($myfile,filesize("webdictionary.txt"));
PHP ปิดไฟล์ - fclose()
ฟังก์ชันfclose()
นี้ใช้เพื่อปิดไฟล์ที่เปิดอยู่
แนวปฏิบัติในการเขียนโปรแกรมที่ดีในการปิดไฟล์ทั้งหมดหลังจากที่คุณทำเสร็จแล้ว คุณไม่ต้องการให้ไฟล์เปิดทำงานบนเซิร์ฟเวอร์ของคุณกินทรัพยากร!
ต้องการ ชื่อfclose()
ของไฟล์ (หรือตัวแปรที่เก็บชื่อไฟล์) ที่เราต้องการปิด:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP อ่านบรรทัดเดียว - fgets()
ฟังก์ชันfgets()
นี้ใช้เพื่ออ่านบรรทัดเดียวจากไฟล์
ตัวอย่างด้านล่างจะแสดงบรรทัดแรกของไฟล์ "webdictionary.txt":
ตัวอย่าง
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
หมายเหตุ:หลังจากการเรียกใช้fgets()
ฟังก์ชัน ตัวชี้ไฟล์ได้ย้ายไปที่บรรทัดถัดไป
PHP ตรวจสอบจุดสิ้นสุดของไฟล์ - feof ()
ฟัง ก์feof()
ชั่นตรวจสอบว่า "จุดสิ้นสุดของไฟล์" (EOF) มาถึงแล้วหรือไม่
ฟังก์ชันfeof()
นี้มีประโยชน์สำหรับการวนซ้ำข้อมูลที่ไม่ทราบความยาว
ตัวอย่างด้านล่างอ่านไฟล์ "webdictionary.txt" ทีละบรรทัด จนกว่าจะถึงจุดสิ้นสุดไฟล์:
ตัวอย่าง
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP อ่านอักขระตัวเดียว - fgetc()
ฟังก์ชันfgetc()
นี้ใช้เพื่ออ่านอักขระตัวเดียวจากไฟล์
ตัวอย่างด้านล่างอ่านไฟล์ "webdictionary.txt" ทีละอักขระ จนกว่าจะถึงจุดสิ้นสุดไฟล์:
ตัวอย่าง
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
หมายเหตุ:หลังจากการเรียกใช้fgetc()
ฟังก์ชัน ตัวชี้ไฟล์จะย้ายไปยังอักขระถัดไป
อ้างอิงระบบไฟล์ PHP ที่สมบูรณ์
สำหรับการอ้างอิงที่สมบูรณ์ของฟังก์ชันระบบไฟล์ ให้ไปที่การ อ้างอิงระบบไฟล์ PHP ทั้งหมด ของ เรา