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

phpfwrite(resource, string, [length]);

Ví dụ

Ghi một chuỗi vào file:

php<?php
  $file = fopen('test.txt', 'w');
  fwrite($file, 'text');
  fclose($file);
?>

Ví dụ

Chỉ ghi 2 ký tự đầu tiên:

php<?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<?php
  $file = fopen('test.txt', 'a');
  fwrite($file, '!');
  fclose($file);
?>

Xem thêm