PHP fopen()ฟังก์ชั่น
❮ การอ้างอิงระบบไฟล์ PHPตัวอย่าง
เปิดไฟล์ อ่านบรรทัด - จนกว่าจะถึง EOF:
<?php
$file = fopen("test.txt", "r");
//Output lines until EOF is reached
while(! feof($file)) {
$line = fgets($file);
echo $line. "<br>";
}
fclose($file);
?>
ความหมายและการใช้งาน
ฟังก์ชั่น fopen() เปิดไฟล์หรือ URL
หมายเหตุ:เมื่อเขียนลงในไฟล์ข้อความ ต้องแน่ใจว่าใช้อักขระลงท้ายบรรทัดที่ถูกต้อง! ระบบ Unix ใช้ \n, ระบบ Windows ใช้ \r\n และระบบ Macintosh ใช้ \r เป็นอักขระลงท้ายบรรทัด Windows เสนอแฟล็กการแปล ('t') ซึ่งจะแปล \n เป็น \r\n เมื่อทำงานกับไฟล์ คุณยังสามารถใช้ 'b' เพื่อบังคับโหมดไบนารี ในการใช้แฟล็กเหล่านี้ ให้ระบุ 'b' หรือ 't' เป็นอักขระตัวสุดท้ายของพารามิเตอร์ mode
ไวยากรณ์
fopen(filename, mode, include_path, context)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
filename | Required. Specifies the file or URL to open |
mode | Required. Specifies the type of access you require to the file/stream. Possible values:
|
include_path | Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well |
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ทรัพยากรตัวชี้ไฟล์เกี่ยวกับความสำเร็จ FALSE และข้อผิดพลาดเกี่ยวกับความล้มเหลว คุณสามารถซ่อนข้อผิดพลาดได้โดยเพิ่ม "@" หน้าชื่อฟังก์ชัน |
---|---|
รุ่น PHP: | 4.3+ |
บันทึกการเปลี่ยนแปลง PHP: | PHP 7.1: เพิ่มตัวเลือก "e" PHP 5.2: เพิ่มตัวเลือก "c" และ "c+" |
❮ การอ้างอิงระบบไฟล์ PHP