在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;