首页 > 技术文章 > PHP 实现下载文件到本地

evai 2016-11-12 18:11 原文

只需要在php文件中设置请求头就可以了,创建download.php文件,代码如下:

$fileName = $_GET['filename']; //得到文件名
header( "Content-Disposition:  attachment;  filename=".$fileName); //告诉浏览器通过附件形式来处理文件
header('Content-Length: ' . filesize($fileName)); //下载文件大小
readfile($fileName);  //读取文件内容

html 代码如下:

<a href="download.php?filename=a.jpg">下载a.jpg</a>

 

推荐阅读