php利用header()实现文件下载

<?php
$file = './test.png';
$filename = basename($file);

//设置头信息
header("Content-Type:application/octet-stream");
header("Content-Disposition:attachment;filename='$filename'");
header("Content-Transfer-Encoding:binary");

//输出文件内容
$handle = fopen($file,'r');
$content = fread($handle, filesize($file));
echo $content;
fclose($handle);

发布了36 篇原创文章 · 获赞 4 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章