首页 > 代码库 > PHP文件下载

PHP文件下载

  • 文件下载
 1 <?php 2     $fileName="tony.jpeg"; 3     //判断文件存在否,如果不存在,程序中断执行 4     if(!file_exists($fileName)){ 5         die("文件不存在"); 6     } 7     //打开文件 8     $fp=fopen($fileName,"r"); 9     //获取下载文件大小10     $fileSize=filesize($fileName);11     //下载文件需要的头12     header("Content-type: application/octet-stream");13     header("Accept-Ranges: bytes");14     header("Accept-Length: $fileSize");15     //客户端弹出对话框,对应的文件名16     header("Content-Disposition: attachment; filename=".$fileName);17     //判断文件是否结束,回送数据18     while(!feof($fp)){19         $contents=fread($fp,$fileSize);20         echo $contents;21     }22     //关闭文件23     fclose($fp);24 ?>

IE8运行结果如下:

技术分享

  •  中文名文件下载,读取字节计数器

当文件名为中文时,会读取不到相应的文件,提示文件不存在,所以我们用iconv函数把utf-8转码成gb2312

 1 <?php 2     //文件名为中文时,需要转码成gb2312显示 3     $fileName="老泪纵横.jpg"; 4     $fileName=iconv("utf-8","gb2312",$fileName); 5     //判断文件存在否,如果不存在,程序中断执行 6     if(!file_exists($fileName)){ 7         die("文件不存在"); 8     } 9     //打开文件10     $fp=fopen($fileName,"r");11     //获取下载文件大小12     $fileSize=filesize($fileName);13     //下载文件需要的头14     header("Content-type: application/octet-stream");15     header("Accept-Ranges: bytes");16     header("Accept-Length: $fileSize");17     //客户端弹出对话框,对应的文件名18     header("Content-Disposition: attachment; filename=".$fileName);19     //判断文件是否结束,回送数据20     //为了安全,我们做一个文件读取字节计数器21     $buffer=1024;22     $fileCount=0;23     while(!feof($fp)&&($fileSize-$fileCount)>0){24         $contents=fread($fp,$buffer);25         //统计读了多少个字节26         $fileCount+=$buffer;27         //把部分数据回送给浏览器28         echo $contents;29     }30     //关闭文件31     fclose($fp);32 ?>

IE8运行结果如下:

技术分享

  •  当下载文件与该php文件不在同一文件夹下时,用相对路径表示如下:
 1 <?php 2     //文件名为中文时,需要转码成gb2312显示 3     $fileName="老泪纵横.jpg"; 4     $fileName=iconv("utf-8","gb2312",$fileName); 5     //相对路径表示文件位置 6     $filePath="./pictures/".$fileName; 7     //判断文件存在否,如果不存在,程序中断执行 8     if(!file_exists($filePath)){ 9         die("文件不存在");10     }11     //打开文件12     $fp=fopen($filePath,"r");13     //获取下载文件大小14     $fileSize=filesize($filePath);15     //下载文件需要的头16     header("Content-type: application/octet-stream");17     header("Accept-Ranges: bytes");18     header("Accept-Length: $fileSize");19     //客户端弹出对话框,对应的文件名20     header("Content-Disposition: attachment; filename=".$fileName);21     //判断文件是否结束,回送数据22     //为了安全,我们做一个文件读取字节计数器23     $buffer=1024;24     $fileCount=0;25     while(!feof($fp)&&($fileSize-$fileCount)>0){26         $contents=fread($fp,$buffer);27         //统计读了多少个字节28         $fileCount+=$buffer;29         //把部分数据回送给浏览器30         echo $contents;31     }32     //关闭文件33     fclose($fp);34 ?>

IE8运行结果如下:

技术分享

  • 用绝对路径表示如下:
1 //绝对路径表示文件位置2 $filePath=$SERVER[‘DOCUMENT_ROOT‘]."./pictures/".$fileName;

结果同上

  •  用函数把上述程序封装
 1 <?php 2     //对函数参数的说明: 3     //$fileName 文件名,格式为 "老泪纵横.jpg" 4     //$fileSubDir  下载文件的子路径,格式为 "./pictures/" 5     function fileDown($fileName,$fileSubDir){ 6         //文件名为中文时,需要转码成gb2312显示 7         $fileName=iconv("utf-8","gb2312",$fileName); 8         //相对路径表示文件位置 9         //$filePath="./pictures/".$fileName;10         //绝对路径表示文件位置11         $filePath=$SERVER[‘DOCUMENT_ROOT‘].$fileSubDir.$fileName;12         //判断文件存在否,如果不存在,程序中断执行13         if(!file_exists($filePath)){14             die("文件不存在");15         }16         //打开文件17         $fp=fopen($filePath,"r");18         //获取下载文件大小19         $fileSize=filesize($filePath);20         //下载文件需要的头21         header("Content-type: application/octet-stream");22         header("Accept-Ranges: bytes");23         header("Accept-Length: $fileSize");24         //客户端弹出对话框,对应的文件名25         header("Content-Disposition: attachment; filename=".$fileName);26         //判断文件是否结束,回送数据27         //为了安全,我们做一个文件读取字节计数器28         $buffer=1024;29         $fileCount=0;30         while(!feof($fp)&&($fileSize-$fileCount)>0){31             $contents=fread($fp,$buffer);32             //统计读了多少个字节33             $fileCount+=$buffer;34             //把部分数据回送给浏览器35             echo $contents;36         }37         //关闭文件38         fclose($fp);39     }40     //调用函数41     //fileDown("老泪纵横.jpg","./pictures/");42     fileDown("tony.jpeg","");43 ?>
  •  页面上有多个文件可以选择下载

fileDownList.php  显示可以下载的文件:

 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <title>可下载文件列表</title> 5 </head> 6 <body> 7 <?php 8     //避免“点击下载”四个字乱码 9     header("Content-Type:text/html;charset=utf-8");10 ?>11     <a href="http://www.mamicode.com/fileDownProcess.php?fileName=hungry.jpg">点击下载</a><img src="http://www.mamicode.com/pictures/hungry.jpg" width="70px" /><br/><br/>12     <a href="http://www.mamicode.com/fileDownProcess.php?fileName=tony.jpeg">点击下载</a><img src="http://www.mamicode.com/pictures/tony.jpeg" width="70px" /><br/><br/>13     <a href="http://www.mamicode.com/fileDownProcess.php?fileName=老泪纵横.jpg">点击下载</a><img src="http://www.mamicode.com/pictures/老泪纵横.jpg" width="70px" /><br/><br/>14     <a href="http://www.mamicode.com/fileDownProcess.php?fileName=hot.jpg">点击下载</a><img src="http://www.mamicode.com/pictures/hot.jpg" width="70px" /><br/><br/>15     <a href="http://www.mamicode.com/fileDownProcess.php?fileName=smile.jpg">点击下载</a><img src="http://www.mamicode.com/pictures/smile.jpg" width="70px" /><br/><br/>16 </body>17 </html>

fileDownProcess.php  完成文件下载:

1 <?php2     require "fileDownService.php";3     header("Content-Type:text/html;charset=utf-8");4     //接收要下载文件的名字5     $fileName=$_REQUEST[‘fileName‘];6     fileDown($fileName,"./pictures/");7 ?>

fileDownService.php  完成下载的函数封装:

 1 <?php 2     //对函数参数的说明: 3     //$fileName 文件名,格式为 "老泪纵横.jpg" 4     //$fileSubDir  下载文件的子路径,格式为 "./pictures/" 5     function fileDown($fileName,$fileSubDir){ 6         //文件名为中文时,需要转码成gb2312显示 7         $fileName=iconv("utf-8","gb2312",$fileName); 8         //相对路径表示文件位置 9         //$filePath="./pictures/".$fileName;10         //绝对路径表示文件位置11         $filePath=$SERVER[‘DOCUMENT_ROOT‘].$fileSubDir.$fileName;12         //判断文件存在否,如果不存在,程序中断执行13         if(!file_exists($filePath)){14             die("文件不存在");15         }16         //打开文件17         $fp=fopen($filePath,"r");18         //获取下载文件大小19         $fileSize=filesize($filePath);20         //下载文件需要的头21         header("Content-type: application/octet-stream");22         header("Accept-Ranges: bytes");23         header("Accept-Length: $fileSize");24         //客户端弹出对话框,对应的文件名25         header("Content-Disposition: attachment; filename=".$fileName);26         //判断文件是否结束,回送数据27         //为了安全,我们做一个文件读取字节计数器28         $buffer=1024;29         $fileCount=0;30         while(!feof($fp)&&($fileSize-$fileCount)>0){31             $contents=fread($fp,$buffer);32             //统计读了多少个字节33             $fileCount+=$buffer;34             //把部分数据回送给浏览器35             echo $contents;36         }37         //关闭文件38         fclose($fp);39     }40 ?>

结果如下:

技术分享

鼠标点击“点击下载”,弹出如下框:

技术分享

点击保存,会弹出保存路径可以选择,如下:

技术分享

 

PHP文件下载