Maison >développement back-end >tutoriel php >php文件下载解决办法
php文件下载
下面是我在网上的找的代码,自己改了下:
down_file.php:
<?php<br /><br />if(!empty($_GET)){<br /><br /> header("Content-type: application/octet-stream ; charset=utf-8"); //输出类型 <br /> header("Content-Disposition:filename=".$name);<br /> header("Accept-Ranges: bytes"); //文件单位<br /> header("Accept-Length:".$filesize); //文件大小<br /> header("Content-Disposition: attachment; filename=".$name);//下载时显示的名字<br />$name=$_GET['name'];<br /> $url=$_GET['url'];<br /><br /> $f=fopen($url.$name, "r");<br /> $buffer=1024;<br /> $filesize=filesize($name);<br /> $downsize=0;<br /> $str="";<br /> while(!feof($f) && ($filesize-$downsize)>0){<br /> $downsize+=$buffer;<br /> echo fread($f,$buffer);<br /><br /> }<br /> //echo $str;<br /> fclose($f);<br /><br />}<br /><br />?>
<!DOCTYPE html><br /><html><br /> <head><br /> <meta charset="utf-8"><br /> <title>Document</title><br /> </head><br /> <body><br /> <br /> <a href="down_file.php?name=a.jpg&url=./img/">下载a.jpg</a><br /><br /> </body><br /></html>
<br />if(!empty($_GET)){<br /> $name=$_GET['name'];<br /> $url=$_GET['url'];<br /> $filesize=filesize($url.$name);<br /><br /> header("Content-type: application/octet-stream ; charset=utf-8"); //输出类型 <br /> header("Content-Disposition:filename=".$name);<br /> header("Accept-Ranges: bytes"); //文件单位<br /> header("Accept-Length:".$filesize); //文件大小<br /> header("Content-Disposition: attachment; filename=".$name);//下载时显示的名字<br /><br /> <br /> $f=fopen($url.$name, "r");<br /> $buffer=1024;<br /> $downsize=0;<br /> $str="";<br /> while(!feof($f) && ($filesize-$downsize)>0){<br /> $downsize+=$buffer;<br /> echo fread($f,$buffer);<br /> <br /> }<br /> //echo $str;<br /> fclose($f);<br /> <br />}<br />