下面是我在网上的找的代码,自己改了下:
down_file.php:
<?php
if(!empty($_GET)){
header("Content-type: application/octet-stream ; charset=utf-8"); //输出类型
header("Content-Disposition:filename=".$name);
header("Accept-Ranges: bytes"); //文件单位
header("Accept-Length:".$filesize); //文件大小
header("Content-Disposition: attachment; filename=".$name);//下载时显示的名字
$name=$_GET['name'];
$url=$_GET['url'];
$f=fopen($url.$name, "r");
$buffer=1024;
$filesize=filesize($name);
$downsize=0;
$str="";
while(!feof($f) && ($filesize-$downsize)>0){
$downsize+=$buffer;
echo fread($f,$buffer);
}
//echo $str;
fclose($f);
}
?>
html页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<a href="down_file.php?name=a.jpg&url=./img/">下载a.jpg</a>
</body>
</html>
目的是下载a.jpg,
问题是点击后弹出窗口下载的是down_file.php而不是a.jpg。。如下图
求指点一下,能给个能用的代码更好了
------解决思路----------------------
$name=$_GET['name'];
$url=$_GET['url'];
$filesize=filesize($url.$name);
这三句要放在上面,否则 header("Content-Disposition:filename=".$name); 会拿不到$name导致出notice.
改成这样
if(!empty($_GET)){
$name=$_GET['name'];
$url=$_GET['url'];
$filesize=filesize($url.$name);
header("Content-type: application/octet-stream ; charset=utf-8"); //输出类型
header("Content-Disposition:filename=".$name);
header("Accept-Ranges: bytes"); //文件单位
header("Accept-Length:".$filesize); //文件大小
header("Content-Disposition: attachment; filename=".$name);//下载时显示的名字
$f=fopen($url.$name, "r");
$buffer=1024;
$downsize=0;
$str="";
while(!feof($f) && ($filesize-$downsize)>0){
$downsize+=$buffer;
echo fread($f,$buffer);
}
//echo $str;
fclose($f);
}