首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP

TP6批量打包下载文件图片

作者:小萝卜 2022-09-28 浏览 627

简介thinkphp6 批量打包下载图片,批量下载图片,实测可用

//批量下载图片 

public function download()
    {
        $data = Db::name( 'legal_customer' )->where( 'id' , 'in' , $this->param['ids'] )->field( 'cus_name,occupied_pdf,pro_pdf' )->select();
        $occupiedPdf = $proPdf = [];
        foreach ( $data as $val ) {
            $occupiedPdf[] = [ 'name' => '【 '.$val['cus_name'].' 】协议PDF地址' , 'url' => $val['occupied_pdf'] ];
            $occupiedPdf[]      = [ 'name' => '【 '.$val['cus_name'].' 】承诺书PDF地址' , 'url' => $val['pro_pdf'] ];
        }
        Download::Download($occupiedPdf);
    }

调用封装方法Download.php

<?php

// +---------------------------------------------------------------------
// | Author: Hang 本地上传服务
// +----------------------------------------------------------------------
namespace app\api\util;

use ZipArchive;
class Download
{

    /**
     * 下载文件
     * @param $img
     * @return string
     */
    public static 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 = self::CurlDownload( $value['url'] );
                if ( $fileContent ) {
                    $__tmp   = self::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 '';
                    }
                    //通过前端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=PDF文件.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 static 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 $error;
        }

        return $res;
    }

    /**
     * 保存临时文件
     * @param $url
     * @param $dir
     * @param $content
     * @return array
     */
    public static 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 [ $path , $extname ];
    }

}


实测结果:

 

很赞哦! (0)

文章评论

    高端网站建设