zoukankan      html  css  js  c++  java
  • php使用http请求头实现文件下载

    众所周知php对http协议的依赖特别强,像java或者asp.net在某些情况下可以不依赖http例如asp.net的winform,对php来说文件下载可以使用http的请求头加上php的IO就可以实现了

    1.先来看下下载所要用的的请求头

     header("Content-type:application/octet-stream");
     header("Accept-Ranges:bytes");
     header("Accept-Length:".$file_Size);
     header("Content-Disposition: attachment; filename=".$filename);


    content-type就不解释了,Accept-Ranges表示接收数据的类型或者范围,图片属于二进制的东西所以需要使用字节的方式传输,Accept-Lenght表示接收的大小,php文件下载需要告诉浏览器下载的文件有多大,最后一个是附件只需要把文件名给过去就可以,这个名称就是下载时显示的文件名称。


    2.php的文件操作出现的比较早,文件名是中文的时候需要注意转码

    $filename=iconv("UTF-8","GB2312",$filename);


    3.php的文件下载机制是首先apcahe把文件信息读入服务器内存,然后使用请求头把文件二进制信息通过浏览器传给客户端

    //判断路径是否存在
         
         if(!file_exists($filepath)){
            echo "文件不存在";
            return;
         }
    
         $fp=fopen($filepath,"r");
    
         //取得文件大小
         $file_Size=filesize($filepath);
          
         header("Content-type:application/octet-stream");
         header("Accept-Ranges:bytes");
         header("Accept-Length:".$file_Size);
         header("Content-Disposition: attachment; filename=".$filename);
    
         $buffer=1024;
         $buffer_count=0;
        
         while(!feof($fp)&&$file_Size-$buffer_count>0){
           $data=fread($fp,$buffer);
            $buffer_count+=$buffer;
           echo $data;
           
         }
       
         fclose($fp);


    feof用来判断文件是否已经读到了末尾,fread用来把文件读入缓冲区,缓冲区的大小是1024,一边读取一边把数据输出到浏览器。为了下载的安全性每次读数据都进行字节的计数。文件读取完毕后关闭输入流


    3.上面的代码有的时候不好使,可以清空(擦掉)输出缓冲区,使用下面的代码即可

    ob_clean();


    来看下运行效果图

    下载之后文件也没有任何的问题

    下面是全部代码

    <?php
    
       function downloadFile($name){
         ob_clean();
        
         $filename=$name;
    
         $filename=iconv("UTF-8","GB2312",$filename);
          
         $filepath="E:\otherphp\download\download\".$filename;
    
        
       
         //判断路径是否存在
         
         if(!file_exists($filepath)){
            echo "文件不存在";
            return;
         }
    
         $fp=fopen($filepath,"r");
    
         //取得文件大小
         $file_Size=filesize($filepath);
          
         header("Content-type:application/octet-stream");
         header("Accept-Ranges:bytes");
         header("Accept-Length:".$file_Size);
         header("Content-Disposition: attachment; filename=".$filename);
    
         $buffer=1024;
         $buffer_count=0;
        
         while(!feof($fp)&&$file_Size-$buffer_count>0){
           $data=fread($fp,$buffer);
            $buffer_count+=$buffer;
           echo $data;
           
         }
       
         fclose($fp);
          
         
    
    
       }
       downloadFile("桌面截图.png");
    ?>


  • 相关阅读:
    关闭编辑easyui datagrid table
    sql 保留两位小数+四舍五入
    easyui DataGrid 工具类之 util js
    easyui DataGrid 工具类之 后台生成列
    easyui DataGrid 工具类之 WorkbookUtil class
    easyui DataGrid 工具类之 TableUtil class
    easyui DataGrid 工具类之 Utils class
    easyui DataGrid 工具类之 列属性class
    oracle 卸载
    “云时代架构”经典文章阅读感想七
  • 原文地址:https://www.cnblogs.com/pangblog/p/3297196.html
Copyright © 2011-2022 走看看