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
readfile(
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
$res = readfile('example.txt');
echo "read bytes: " . $res;
?>
Ví dụ
Thử đọc một file không tồn tại:
<?php
$res = readfile('nonexistent.txt');
if ($res === false) {
echo "Failed to read file";
}
?>
Ví dụ
Sử dụng cờ include_path:
<?php
$res = readfile('config.ini', true);
echo $res !== false ? "Read success" : "Read failed";
?>
Xem thêm
-
hàm
file_get_contents,
đọc file vào một chuỗi -
hàm
fpassthru,
xuất phần còn lại của file -
hàm
file,
đọc file vào một mảng