Home  >  Article  >  Backend Development  >  Code example of Thinkphp batch downloading pictures and files

Code example of Thinkphp batch downloading pictures and files

不言
不言Original
2018-08-04 14:06:123796browse

This article brings you code examples about Thinkphp batch downloading of images and files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

//批量下载图片
    public function download_img_all(){
        $dfile = tempnam('./public/upload/qr_code/', 'tmp');//产生一个临时文件,用于缓存下载文件
        $zip = new Zipfile();
        $filename = 'erweima'.'.zip'; //下载的默认文件名
        // $image  = M('Device')->field('erweima')->select();
        // foreach($image as $v){
        //     $image_path = './public/upload/qr_code/';方法一
        //      $zip->add_path($image_path);
        //      // $zip->add_file(file_get_contents($v['erweima']), $v['erweima']);
        //      // 添加打包的图片,第一个参数是图片内容,第二个参数是压缩包里面的显示的名称, 可包含路径
        //      // 或是想打包整个目录 用 
        // }
        $image_path = './public/upload/qr_code/';//方法二
        $zip->add_path($image_path);
        $zip->output($dfile);
  // 下载文件
        ob_clean();
        header('Pragma: public');
        header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        header('Content-type:multipart/form-data');
        header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
        header('Content-length:'. filesize($dfile));
        $fp = fopen($dfile, 'r');
        while(connection_status() == 0 && $buf = @fread($fp, 8192)){
         echo $buf;
        }
        fclose($fp);
        @unlink($dfile);
        @flush();
        @ob_flush();
        exit();
    }

Recommended related articles:

How does PHP decompress the compressed package file to the specified directory? (Pure code)

How to delete PHP Code examples for the directory and all files in the directory

The above is the detailed content of Code example of Thinkphp batch downloading pictures and files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn