PHP reads the address in excel to download multiple files,

Source: Internet
Author: User
Tags fread ziparchive

PHP reads the address in excel to download multiple files,

PHP file downloads can be divided into single files and multi-files. If you want to write a single file, but you want to download multiple files cyclically, I did not succeed in the experiment. The following describes how to download a single file:

 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     }  

For multiple files, you can download them by compressing them. The following Code reads the file address in excel and packages the downloaded source code:

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 require '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 the first worksheet 11 12 $ arr = $ objPHPE Xcel-> 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 // transmit only 1024 bytes of data at a time to the client 51 $ buffer = 1024; // 52 While (! Feof ($ file) {53 // read the file into memory 54 $ file_data = fread ($ file, $ buffer ); 55 // each time 1024 bytes of data are returned to the client 56 echo $ file_data; 57} 58 fclose ($ file); 59 unlink ($ filename );

Shows the excel content:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.