前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP实现打包下载文件的方法示例

PHP实现打包下载文件的方法示例

作者头像
用户2323866
修改2021-07-09 11:02:40
8320
修改2021-07-09 11:02:40
举报
文章被收录于专栏:技术派

本文实例讲述了PHP实现打包下载文件的方法。分享给大家供大家参考,具体如下:

/**

  • 下载文件
  • @param $img
  • @return string */

public function Download($img) { $items = []; $names = []; if($img) { //用于前端跳转zip链接拼接 $path_redirect = '/zip/'.date('Ymd'); //临时文件存储地址 $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(); /【要记得博客地址www.isres.com】/ $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 ''; } //通过前端js跳转zip地址下载,让不使用php代码下载zip文件 //if (file_exists($zipname)) { //拼接附件地址 //$redirect = 域名.$path_redirect.'/'.$filename; //return $redirect; //header("Location:".$redirect); //} //直接写文件的方式下载到客户端 if (file_exists($zipname)) { header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=附件.zip'); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($zipname)); //告诉浏览器,文件大小 @readfile($zipname); } //删除临时文件 @unlink($zipname); } } return ''; } } /**

  • curl获取链接内容
  • @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; } /**

  • 保存临时文件
  • @param $url
  • @param $dir
  • @param $content
  • @return array */

public function SaveFile( $url ,$dir , $content) { $fname = basename($url); //返回路径中的文件名部分 $str_name = pathinfo($fname); //以数组的形式返回文件路径的信息 $extname = strtolower($str_name['extension']); //把扩展名转换成小写 $path = $dir.'/'.md5($url).$extname; $fp = fopen( $path ,'w+' ); fwrite( $fp , $content ); fclose($fp); return array( $path , $extname) ; }

引用:

$img = [['url'=>'地址url/1.jpg','name'=>'名字']]; Download($img);

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档