批量下载(二)

      通过上篇文章测试发现一个问题, 由于以导出excel形式将所有下载的数据汇总到一起,会出现数据量过大时超出了内存的限制。

解决方法一:

根据要下载的数据量的大小,手动设置内存大小。说明:php.ini 默认内存为128M
ini_set('memory_limit', '512M');

解决方法二:

将所有要下载的卡号文件以压缩包的方式下载到本地。

Card.php

if($action == 'batchDownload') {
    $downloadList = $_POST['downloadList'];
    if(is_array($downloadList)) {
        _debug($downloadList);
        $ret = $Card ->batchDownload($downloadList);
        if(isset($ret['status']) && $ret['status'] == 1) {
            //ok
        } else {
            $msg[] = '批量下载失败: ' . $ret['error'];
        }
    } else {
        $msg[] = '先选择要下载的卡号';
    }
}

Card.class.php

class Card {
     /**
     * 批量下载
     * @param array $download_list
     *     array( [1560392489_2_2_30000.txt] => on
     *               [1560392487_2_2_30000.txt] => on
     *               [1560392486_2_2_30000.txt] => on )
     * @return array(status => int, error => string)
     */
    public function batchDownload($download_list) {
        $ret = array('status' => 0, 'error' => '');
        $copy_flag = true;
        $tar_flag = false;
        $token = genToken();
        $work_dir = '/tmp/' . $token;
        $tar_filename = 'card.tar.gz';
        $tar_filename2 = '/tmp/' . $tar_filename;
        //创建临时目录
        if(mkdir($work_dir)) {
            //拷贝文件并重命名
            foreach ($download_list as $filename => $value) {
                $file = $this->getDir() . '/'. $filename;
                $card_arr = explode('_', $filename);
                $f_type = $card_arr[2];
                $f_type_name = isset($this ->card_type[$f_type]) ? $this ->card_type[$f_type] : 'unknown';
                $new_filename = $f_type . '_' . $f_type_name . '_' . $filename;
                $new_file = $work_dir . '/' . $new_filename;
                if( ! copy($file, $new_file) ) {
                    $ret = array('status' => 0, 'error' => $filename.'文件复制失败');
                    $copy_flag = false;
                    break;
                }
            }
            //打包
            if($copy_flag) {
                $cmd = 'cd /tmp/ && tar -zcf ' . $tar_filename . ' ' . $token . '/*.txt ';
                _info($cmd);
                $output = array();
                $return_var = null;
                exec($cmd, $output, $return_var);
                $output_str = implode("\n", $output);
                if($return_var === 0) {
                    $tar_flag = true;
                } else {
                    $error = "执行命令{$cmd}出错,msg:" . $output_str;
                    _error($error);
                    $ret = array('status' => 0, 'error' => '打包失败');
                }
            }
            //删除临时目录
            if ($handle = opendir($work_dir)) {
                while (false !== ($file = readdir($handle))) {
                    $file2 = $work_dir . '/' . $file;
                    if(is_file($file2)) {
                        unlink($file2);
                    }
                }
            }
            rmdir($work_dir);
            //下载
            if($tar_flag) {
                browserDownloadFile($tar_filename2, 'force-download');
            }
        } else {
            $ret = array('status' => 0, 'error' => '新建目录失败');
        }
        return $ret;
    }
}
/**
 * 让客户端去下载一个文件
 */
function browserDownloadFile($filename, $type = "text", $new_filename='')
{
    if(empty($new_filename)) {
        $basename = basename($filename);
    } else {
        $basename = $new_filename;
    }
    header('Content-type: application/'. $type);
    header('Content-Disposition: attachment; filename="' . $basename . '"');
    readfile($filename);
    exit();
}

结果如下:

友情提示:若WinRAR解压后的文件出现中文乱码,有两种情况:

1.WinRAR版本过低,需升级版本

2.下载好压压缩文件

posted @ 2019-06-13 17:57  lonmy  阅读(211)  评论(0编辑  收藏  举报