Download multiple files by reading the address in excel in PHP

Keywords: PHP Excel

;

PHP file download can be divided into single file and multi file. If it is a single file writing method, it can be realized, but if I want to download multiple files in a circular way, my experiment is not successful. First, download the single file as follows:

 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     }  

If there are multiple files, you can download them by compression. The following code is the source code to read the file address in excel and package and download it:

 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); // Read first sheet
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 //Only 1024 bytes of data are transmitted to the client at a time
51 $buffer = 1024; //
52 while (!feof($file)) {
53     //Read files into memory
54     $file_data = fread($file, $buffer);
55     //Send back 1024 bytes of data to the client at a time
56     echo $file_data;
57 }
58 fclose($file);
59 unlink($filename);

The content of excel is shown in the following figure:

Packaging process reference: https://www.cnblogs.com/shaoyikai/p/3755079.html

Posted by reneeshtk on Mon, 04 May 2020 18:04:41 -0700