code example for PHP to implement packaged download files

Source: Internet
Author: User
Tags temporary file storage ziparchive
This article mainly introduces the PHP implementation of the package download file method, involving PHP based on Curl file acquisition, traversal, zip compression and download and other related operations skills, the need for friends can refer to the following

This article describes the PHP implementation of the package download file method. Share to everyone for your reference, as follows:


/*** Download File * @param $img * @return string*/public function Download ($img) {$items = [];    $names = [];      if ($img) {//For front-end jump zip link stitching $path _redirect = '/zip/'. Date (' Ymd ');      Temporary file storage Address $path = '/tmp '. $path _redirect;      if (!is_dir ($path)) {mkdir ($path, 0777,true);        } foreach ($img as $key = + $value) {$fileContent = ';        $fileContent = $this->curldownload ($value [' url ']);          if ($fileContent) {$__tmp = $this->savefile ($value [' url '], $path, $fileContent);          $items [] = $__tmp[0]; $names [] = $value [' name ']. ' _'. ($key + 1). '. '.        $__TMP[1];        }} if ($items) {$zip = new ziparchive (); $filename = Time (). '        Download.zip '; $zipname = $path. '        /'. $filename; if (!file_exists ($zipname)) {$res = $zip->open ($zipname, Ziparchive::create |          Ziparchive::overwrite);          if ($res) {foreach ($items as $k = = $v) {    $value = Explode ("/", $v);              $end = End ($value);              $zip->addfile ($v, $end);            $zip->renamename ($end, $names [$k]);          } $zip->close ();          } else {return '; }//through the front-end JS jump zip address download, let not use PHP code download ZIP file//if (file_exists ($zipname)) {//Splicing attachment address//$re Direct = domain name. $path _redirect. '            /'. $filename;            return $redirect;          Header ("Location:". $redirect);            }//write the file directly to the client if (file_exists ($zipname)) {header ("cache-control:public");            Header ("Content-description:file Transfer"); Header (' Content-disposition:attachment; filename= attachment. zip '); FileName header ("Content-type:application/zip"); Header in zip format ("content-transfer-encoding:binary"); Tell the browser that this is the header of the binary file (' content-length: '. FileSize ($zipname));          Tell the browser that the file size @readfile ($zipname);         } Delete temporary file @unlink ($zipname);    }} "return";    }}/*** Curl Gets the link content * @param $url * @return mixed|string*/public function curldownload ($url) {$ch = Curl_init ($url);    curl_setopt ($ch, Curlopt_ipresolve, CURL_IPRESOLVE_V4);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_header, 0);    curl_setopt ($ch, Curlopt_connecttimeout, 20);    curl_setopt ($ch, Curlopt_ssl_verifypeer, false);    curl_setopt ($ch, Curlopt_ssl_verifyhost, false);    $errno = Curl_errno ($ch);    $error = Curl_error ($ch);    $res =curl_exec ($ch);    Curl_close ($ch);    if ($errno >0) {return '; } return $res;}    /*** Save temporary file * @param $url * @param $dir * @param $content * @return array*/public function SaveFile ($url, $dir, $content) { $fname = basename ($url); Returns the file name portion of the path $str _name = PathInfo ($fname); Returns the file path information as an array $extname = Strtolower ($str _name[' extension ');    Change the extension to lowercase $path = $dir. '/'. MD5 ($url). $extname; $fp = fopen ($patH, ' w+ ');    Fwrite ($fp, $content);    Fclose ($FP); Return Array ($path, $extname);}

Reference:


$img = [[' url ' = = ' address url/1.jpg ', ' name ' = ' = ' name ']];D ownload ($img);

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.