Hàm readfile

Hàm readfile đọc file và ngay lập tức gửi nội dung của nó đến bộ đệm đầu ra. Nó trả về số byte đã đọc hoặc false trong trường hợp lỗi. Tham số đầu tiên - đường dẫn đến file, tham số thứ hai (tùy chọn) - cờ tìm kiếm file trong include_path, tham số thứ ba (tùy chọn) - ngữ cảnh luồng.

Cú pháp

phpreadfile(
  string $filename,
  bool $use_include_path = false,
  ?resource $context = null
): int|false

Ví dụ

Xuất nội dung của một file văn bản:

php<?php
  $res = readfile('example.txt');
  echo "read bytes: " . $res;
?>

Ví dụ

Thử đọc một file không tồn tại:

php<?php
  $res = readfile('nonexistent.txt');
  
  if ($res === false) {
    echo "Failed to read file";
  }
?>

Ví dụ

Sử dụng cờ include_path:

php<?php
  $res = readfile('config.ini', true);
  echo $res !== false ? "Read success" : "Read failed";
?>

Xem thêm