การเขียน tex file ด้วย php
สิ่งที่ต้อง คำ นึง คือ ไฟล์ ที่เรากำลังจะเขียน สามารถ เขียนได้ หรือ เปล่า ( server unix )
สามารถ เช็คได้ ดัง นี้ คือ เรียก function is_writable()
<?php
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
?>
การ เขียนไฟล์ เราต้อง ให้ function ในการ เปิดไฟล์ คือ fopen() ระบุ ชนิดการ เปิดไฟล์ เป็น w หรือ a เช่น
$fp = fopen("textfile_name.txt", "w"); // เป็นการเปิดไฟล์ เพื่อ เขียน
แล้ว ใช้ fwrite() เขียน ข้อมูล ลงไฟล์ ..
The code
<?php
$your_data = "This is the data to be stored in the text file.";
$fp = fopen("textfile_name.txt", "w");
fwrite($fp, $your_data);
fclose($fp);
?>
สำหรับ php 5 มี function ที่ช่วยให้เรา เขียนไฟล์ ได้เร็ว ขึ้นคือ
int file_put_contents ( string filename, mixed data [, int flags [, resource context]] )
ลองใช้กันดูนะครับ ฟังก์ชัน file_put_contents() : ใช้ในการเขียน ข้อความ ลงไฟล์