zoukankan      html  css  js  c++  java
  • PHP读取excel中地址实现多文件下载

    PHP文件下载有单文件和多文件之分,如果是单文件写个方法可以实现,但是如果想循环下载多个文件我试验是没有成功。先说单文件的下载,方法如下:

     1 function downfile($fileurl) {  
     2         $filenameexplode=explode('/',$fileurl);
     3         $fileRealName=$filenameexplode[count($filenameexplode)-1];
     4         $filename=$fileurl;
     5         $file  =  fopen($filename, "rb");
     6         Header( "Content-type:  application/octet-stream ");   
     7         Header( "Accept-Ranges:  bytes ");   
     8         Header( "Content-Disposition:  attachment;  filename= ".$fileRealName);   
     9         $contents = "";  
    10         while (!feof($file)) {  
    11              $contents .= fread($file, 8192);  
    12         }  
    13         echo $contents;  
    14         fclose($file);   
    15     }  

    如果是多个文件,可以采用压缩下载的方式进行下载,以下代码是读取excel中文件地址并打包下载的源码:

     1 set_time_limit(0);
     2 ini_set("max_execution_time", 0);
     3 ini_set("memory_limit", "1G");
     4 require_once 'Excel/PHPExcel/IOFactory.php';
     5 $filePath ='pdforimg.xlsx';
     6 $fileType = PHPExcel_IOFactory::identify($filePath);
     7 $objReader = PHPExcel_IOFactory::createReader($fileType);
     8 $objPHPExcel = $objReader->load($filePath);
     9 
    10 //$sheet = $objPHPExcel->getSheet(0); // 读取第一個工作表
    11 
    12 $arr = $objPHPExcel->getActiveSheet()->toArray();
    13 foreach($arr as $k=>$v){
    14     if($k>=2){
    15         foreach($arr[0] as $kk=>$vv){
    16             $isnull=str_replace(' ','',$v[0]);
    17             if(!($isnull) || $isnull=="  "){
    18                 break;
    19             }
    20             $data[$k-2][$arr[0][$kk]]=$v[$kk];
    21         }
    22     }
    23 }
    24 
    25 $filename = 'tmp.zip';
    26 
    27 $zip = new ZipArchive();
    28 $zip->open($filename, ZipArchive::OVERWRITE);
    29 
    30 foreach ($data as $key=>$vo) {
    31     if($key>=1000 && $key<1500){
    32         $filenameexplode=explode('/',$vo['url']);
    33         $fileRealName=$filenameexplode[count($filenameexplode)-1];
    34         $fileData = file_get_contents($vo['url']);
    35         if ($fileData) {
    36             $zip->addFromString($fileRealName, $fileData);
    37         }
    38     }
    39 
    40 }
    41 
    42 
    43 $zip->close();
    44 
    45 $file = fopen($filename, "r");
    46 Header("Content-type: application/octet-stream");
    47 Header("Accept-Ranges: bytes");
    48 Header("Accept-Length: " . filesize($filename));
    49 Header("Content-Disposition: attachment; filename=pdf.zip");
    50 //一次只传输1024个字节的数据给客户端
    51 $buffer = 1024; //
    52 while (!feof($file)) {
    53     //将文件读入内存
    54     $file_data = fread($file, $buffer);
    55     //每次向客户端回送1024个字节的数据
    56     echo $file_data;
    57 }
    58 fclose($file);
    59 unlink($filename);

    excel内容如下图所示:

    打包过程参考:https://www.cnblogs.com/shaoyikai/p/3755079.html

  • 相关阅读:
    XBee Level Shifting
    5V and 3V Level Translators
    Short Circuit Protection Circuit
    Non-Inverting Level Shifter : +/-5V signal into a 0 to 3.3V
    7407 74LS07 74LV07 74LVC07
    xvcd – The Xilinx Virtual Cable Daemon
    74HC125 74HCT125 74LV125 74LVC125
    SQL Server全文搜索
    分享今天在客户那里遇到的SQLSERVER连接超时以及我的解决办法
    关于《SQLSERVER走起》微信账号自动回复功能的升级
  • 原文地址:https://www.cnblogs.com/pdandan/p/8269056.html
Copyright © 2011-2022 走看看