ZipArchive扩展的使用和Guzzle依赖的安装使用

在项目开发的过程中,需要去远程下载录音文件 然后保存到自己的项目中,然后再把录音文件压缩打包,最后再下载给用户

1.Guzzle依赖的安装

guzzle官方文档:http://guzzle-cn.readthedocs.io/zh_CN/latest/overview.html#installation

安装

推荐使用 Composer 安装Guzzle,Composer是PHP的依赖管理工具,允许你在项目中声明依赖关系,并安装这些依赖。

# 安装 Composer
curl -sS https://getcomposer.org/installer | php

你可以使用composer.phar客户端将Guzzle作为依赖添加到项目:

php composer.phar require guzzlehttp/guzzle:~6.0

或者,你可以编辑项目中已存在的composer.json文件,添加Guzzle作为依赖:

 {
   "require": {
      "guzzlehttp/guzzle": "~6.0"
   }
}

安装完毕后,你需要引入Composer的自动加载文件:

require 'vendor/autoload.php';

你可以在 getcomposer.org 发现更多关于怎样安装Composer、配置自动加载以及其他有用的东西。

2.Guzzle的使用

学习源头:https://segmentfault.com/q/1010000006174785

具体使用:https://blog.csdn.net/qq_36663951/article/details/80256709

use GuzzleHttpClient;
use GuzzleHttpExceptionGuzzleException;

$client = new Client(['verify' => false]);  //忽略SSL错误
$response = $client->get($url, ['save_to' => public_path($file)]);  //保存远程url到文件

安装

//前提是在composer.json加入"guzzlehttp/guzzle": "6.*",然后composer update

 

3.ZipArchive扩展的使用

我这里的使用方法是 先把远程的文件下载到本地的一个文件夹中,然后再把该文件夹遍历 添加到要生成的压缩文件中去

下载压缩代码

/**
     * ajax 下载录音
     */
    public function download(Request $request)
    {
        // 这里的录音文件的后缀是wav的格式 目前是写死的
        $id = $request->id; // 数据包id
        $res = DB::table('a_data')->where('data_status', 4)->where('data_packet_id', $id)->pluck('record');
        $data_res = DB::table('a_data')->where('data_status', 4)->where('data_packet_id', $id)->pluck('id');

        $path =  Config::get('constants.RECORD_ZIP');

        $filepath = $path.'/'."{$id}";
        $filename = $path.'/'."{$id}.zip";
        $basepath = public_path(Config::get('constants.RECORD_ZIP')).'/'."{$id}";
        if (!file_exists($basepath)) {
            mkdir($basepath, 0755, true);
        }
        if (!file_exists($filename)) {
            $zip = new ipArchive();
            if ($zip->open($filename, ipArchive::CREATE) == TRUE) {
                foreach ($res as $key => $val) {
                    try{
                        $client = new Client(['verify' => false]);  //忽略SSL错误
                        if (!file_exists($basepath.'/'.$data_res[$key].'.wav')) {
                            $response = $client->get($val, ['save_to' => $basepath.'/'.$data_res[$key].'.wav']);  //保存远程url到文件
                        }
                    } catch (GuzzleHttpRequestException $e) {
                        echo 'fetch fail';
                    }
                }
//                dd($filepath, $basepath, $id);
                $this->addFileToZip($filepath, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
                $zip->close();
            }
        }
        if (!file_exists($filename)) {
            exit("无法找到文件");
        }
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($filename)); //文件名
        header("Content-Type: application/zip"); //zip格式的
        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
        header('Content-Length: ' . filesize($filename)); //告诉浏览器,文件大小
        @readfile($filename);
        if (!empty($filename)) {//删除在服务器上生成的zip文件
            unlink($filename);
        }
        // 打包下载结束

        return Storage::download($filename);
    }

    /**
     * 将文件夹打包成zip文件
     */
    private function addFileToZip($path, $zip)
    {
//        dd($path);
        $handler = opendir($path); //打开当前文件夹由$path指定。
        /*
        循环的读取文件夹下的所有文件和文件夹
        其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,
        为了不陷于死循环,所以还要让$filename !== false。
        一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环
        */
        while (($filename = readdir($handler)) !== false) {
            if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’,不要对他们进行操作
                if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归
                    $this->addFileToZip($path . "/" . $filename, $zip);
                } else { //将文件加入zip对象
                    $zip->addFile($path ."/" . $filename);
                }
            }
        }
        @closedir($path);
    }

5.laravel的下载使用

学习源头:http://laravelacademy.org/post/200.html

官方文档:http://laravelacademy.org/post/623.html

return Storage::download($filename);
原文地址:https://www.cnblogs.com/djwhome/p/9265223.html