download.php

<?php
//获取传递的文件名
$filename = $_GET['filename'];
$pathname = "$./download/$filename";
//以只读方式打开文件
$handle = fopen($pathname,"rb");
//返回数据的内容类型
//'octet-stream'八位二进制流
//'content-desposition:attachment'以附件形式打开
header("content-type:application/octet-stream");
header("content-despositon:attachment;filename=$filename");
//循环读取指定大小的内容,并输出
while($str=fread($handle,1024))
{
echo $str;
}
//关闭文件
fclose($handle);
?>

download.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="download.php?filename=test.jpg">下载图片</a>
<br>
<a href="download,php?filename=test.mp4">下载视频</a>
</body>
</html>

发表评论

Close Menu