PHP强制下载文件
//有時(shí)我們不想讓瀏覽器直接打開文件,如PDF文件,而是要直接下載文件,那么以下函數(shù)可以強(qiáng)制下載文件
//函數(shù)中使用了application/octet-stream頭類型。
function downloads($filename,$dir='./')
{$filepath = $dir.$filename;if (!file_exists($filepath)) {header("Content-type: text/html; charset=utf-8");echo "File not found!";exit;} else {$file = fopen($filepath, "r");Header("Content-type: application/octet-stream");Header("Accept-Ranges: bytes");Header("Accept-Length: " . filesize($filepath));Header("Content-Disposition: attachment; filename=".$filename);echo "正在下載".fread($file, filesize($filepath));}}
downloads('a/a.sql');
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: PHP获取当前页面URL
- 下一篇: php截取字符串,带中文,多余的省略号代