php文件下载
时间:2023-3-1 21:50 作者:wen 分类: PHP
<?php
//文件下载头文件
function download($url="img"){
$fileName = $_GET["fileName"];
//拼接好文件的路径
$filePath = "$url/".$fileName;
if(!file_exists($filePath)){
echo "文件未找到,下载失败";
exit;
}
$fp = fopen($filePath,"rb");
//图片在windows系统下面GGK编码,PHP文件是utf-8编码
//通常需要先将php文件的编码修改为GBK
$filePath = iconv("utf-8","gbk",$filePath);
$fileSize = filesize($filePath);
//开始下载,通过header头信息告诉浏览器我向你回应的是文件资源
header("Content-Type:application/octet-stream");
//显示下载的文件进度,以字节的形式回应
header("Accept-Ranges:bytes");
//显示下载的文件的大小
header("Content-Length:".$fileSize);
//以附件的形式显示文件
header("Content-Disposition:attachment;filename=".$fileName);
//开始读取文件资源并回应给浏览器
while(!feof($fp)){
$data = fread($fp,1024);
echo $data;
}
fclose($fp);
}
?>
标签: PHP基础