PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

关于php使用header实现下载功能解决方案

原创
2016-06-13 12:03:17 530浏览

关于php使用header实现下载功能
test1.php关键代码,这里省略链接数据库相关代码,只有从数据库中读出相关代码
$sql_select = "SELECT * FROM `file_information` order by id DESC";
$sql_mysql = mysql_query($sql_select,$connected);
?>







while($sql_fetch = mysql_fetch_array($sql_mysql))
{
?>

    
    
    
    
    
    

}
?>
ID缩略图简介路径长传时间操作
" width="80" heihgt="80"/>
    ">下载



test2.php代码:(由于header("Content-Type: text/html;charset=UTF-8")给予了类型,所以其他html代码全部删除了,并且没有将test2.php中的php代码写在test1.php中,因为test1.php有有输出,会影响header()设置类型
header("Content-Type: text/html;charset=UTF-8");
if(!empty($_GET['download_id']))
{
$download_id = $_GET['download_id'];
$download_id = explode("@@@",$download_id);

$download_path = $download_id[1];
if(!is_null($download_path))
{
$filename = basename($download_path);//获取文件名
$download_file = fopen($download_path,"r");
if($download_file)
{
header("Content-Type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-Length:".filesize($download_path));
header("Content-Disposition: attachment; filename=".$filename);
echo fread($download_file,filesize($download_path));
fclose($download_file);
exit;
}
else
{
echo "The file must be not exist";
}

}
}
?>


这么做其实是可以的,而且下载功能也可以执行,经过测试,在IE9,chrome,firefox上面都可以运行,jpg文件,png文件,txt文件,swf文件都可以下载并查看,但是基于chrome内核的360极速浏览器确出现问题,当图片是png格式时,可以下载并打开,当图片是jpg格式时,可以下载,大小却为0B,打不开图片,这应该是浏览器问题吧?不过chrome都可以为什么它不可以??求好银不思赐教why,thank you!(ps:360极速浏览器的极速模式,兼容模式,IE9模式均不可以下载jpg格式图片)
------解决方案--------------------
if(file_exists($download_file)){
//
}
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。