如何创建并打开正在建立的zip文件?

时间:2018-02-04 23:51:55

标签: php cakephp zip

即使尚未在服务器中创建zip文件,当我们点击下载页面时,我需要能够立即打开zip文件。

有没有办法做到这一点?因为目前需要花费大量时间才能让页面开始下载zip文件。

我也遇到了超时问题,因为构建zip需要花费太多时间。

 $this->loadModel('JobFiles');
    $this->loadComponent('Images');

    $job_nbr = '40819-o';
    $purpose = 'client';

    if (isset($this->request->data['type']) && $this->request->data['type'] != '') {

        $type = $this->request->data['type'];
        $my_files = $this->JobFiles
        ->find()
        ->where([
            'JobFiles.job_number' => $job_nbr,
            'JobFiles.type' => $type,
            'JobFiles.purpose' => $purpose
        ]);

    } else {

        $my_files = $this->JobFiles
        ->find()
        ->where([
            'JobFiles.job_number' => $job_nbr,
            'JobFiles.purpose' => $purpose
        ]);

    }

    $files = array();

    $key = 0;
    foreach( $my_files as $file ){
        // If photo and extension are jpg we provide other files type
        if ($file->type == 'photos' && $file->extension == 'jpg' || $file->extension == 'JPG'):
            $c = $file->ObjectURL;
            $file_headers = @get_headers($c);
            if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
                $exists = false;
            }
            else {
                $files['photos'][$key]['original_file'] = $file->ObjectURL;
                $files['photos'][$key]['name'] = $file->filename;
                $files['photos'][$key]['type'] = $file->type;
                $files['photos'][$key]['id'] = $file->id;
                $key++;
            }
        endif;
        if ($file->type == 'floorplan'):
            $files['floorplan'][$key]['original_file'] = $file->ObjectURL;
            $key++;
        endif;
        if ($file->type == 'video'):
            $files['video'][$key]['original_file'] = $file->ObjectURL;
            $key++;
        endif;
    }

    $zipname = "watherver.zip";
    $zip = new ZipArchive(); 
    $zip_full_path_name = \Cake\Core\Configure::read('pathTo') . '/webroot/img/tmp/zip/' .  $zipname;

    $zip->open($zip_full_path_name, ZIPARCHIVE::CREATE);
    foreach ($files['photos'] as $file) {
        $context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
        $parsed_file = $file['original_file'];
        $download_file = file_get_contents($parsed_file, false,$context);
        $zip->addFromString('photos/original/' . basename($parsed_file), $download_file);
    }

    $zip->close();

    //Set headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename='" . $zipName . "'");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zipName));

干杯,

0 个答案:

没有答案