Hàm fwrite
Hàm fwrite ghi dữ liệu vào một file đã được mở bằng fopen. Tham số đầu tiên của hàm là con trỏ tới file, tham số thứ hai - chuỗi cần ghi, và tham số thứ ba (không bắt buộc) - số byte tối đa sẽ ghi.
Cú pháp
fwrite(resource, string, [length]);
Ví dụ
Ghi một chuỗi vào file:
<?php
$file = fopen('test.txt', 'w');
fwrite($file, 'text');
fclose($file);
?>
Ví dụ
Chỉ ghi 2 ký tự đầu tiên:
<?php
$file = fopen('test.txt', 'w');
fwrite($file, 'text', 2);
fclose($file);
?>
Ví dụ
Thêm văn bản vào cuối file:
<?php
$file = fopen('test.txt', 'a');
fwrite($file, '!');
fclose($file);
?>
Xem thêm
-
hàm
fread,
dùng để đọc từ file -
hàm
file_put_contents,
dùng để ghi dữ liệu vào file