在PHP中,readfile函数用于读取一个文件,并将其内容输出到输出缓冲区。以下是一些readfile函数的使用技巧:
$file = "example.txt";
readfile($file);
$file = "example.txt";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
exit;
$file = "example.txt";
$download_rate = 1000; // 1kb/s
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
$file = fopen($file, "r");
while(!feof($file)) {
echo fread($file, $download_rate);
flush();
sleep(1);
}
fclose($file);
exit;
}
$file = "example.txt";
$content = file_get_contents($file);
echo $content;