【十八】php文件下载源码

index.php

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5     <meta charset="utf-8">
 6 </head>
 7 <body>
 8     <a href="downserver.php?filename=哈哈.JPG">点击下载</a>
 9     <img src='/object-oriented/down/哈哈.JPG' width='100px'>
10     <br/>
11     <a href="downserver.php?filename=哈哈.JPG">点击下载</a>
12     <img src='/object-oriented/down/哈哈.JPG' width='100px'>
13     <br/>
14     <a href="downserver.php?filename=哈哈.JPG">点击下载</a>
15     <img src='/object-oriented/down/哈哈.JPG' width='100px'>
16     <br/>
17     <a href="downserver.php?filename=哈哈.JPG">点击下载</a>
18     <img src='/object-oriented/down/哈哈.JPG' width='100px'>
19     <br/>
20 </body>
21 </html>

downserver.php

1 <?php
2     require('file_down.php');
3     $filename=$_REQUEST['filename'];
4     downfile($filename);
5 ?>

file_down.php

 1 <?php
 2     function downfile($file_name){
 3     // $file_name="哈哈.JPG";
 4     //给文件名转码
 5     $file_name=iconv("utf-8", "gb2312", $file_name);
 6     $file_path="./down/".$file_name;
 7     if (!file_exists($file_path)) {
 8         echo "文件不存在";
 9         return;
10     }
11     $fp=fopen($file_path, "r");
12     $file_size=filesize($file_path);
13     //  下载需要的头文件
14     header("Content-type:application/octed-stream");
15     header("Accept-Ranges:bytes");
16     header("Accept-Lenght:$file_size");
17     header("Content-Disposition:attachment;filename=".$file_name);
18     $buffer=1024;
19     $file_count=0;
20     //读取文件内容
21     while (!feof($fp) &&($file_size-$file_count>0)) {
22         $file_data=fread($fp, $buffer);
23         echo $file_data;
24         $file_count+=$buffer;
25     }
26     fclose($fp);
27 }
28 // downfile("哈哈.JPG");
29 ?>

打印结果:

 

posted @ 2017-11-27 12:25  花花妹子。  阅读(576)  评论(0编辑  收藏  举报