如何创建一个zip存档文件进行下载

时间:2019-04-09 08:09:27

标签: php download zip

在php中创建并下载zip存档。我将通过ajax创建zip归档文件,但在创建并下载带有静态数据zip的URL时未创建zip文件。

foreach($orderItems->payload as $item){
        $zip = new ZipArchive();
        $filename = "./Invoice.zip";

            if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
                exit("cannot open <$filename>\n");
            }
            $direc = pathinfo($item->invoicePath);
            $dir = $direc['dirname'];
            //print_r($dir);exit;
            if (is_dir($dir)){
                //print_r($dir);exit;
                if ($dh = opendir($dir)){
                    while (($file = readdir($dh)) !== false){
                    // If file
                    if (is_file($dir.$file)) {
                        if($file != '' && $file != '.' && $file != '..'){
                        //print_r($newdir);exit;
                            $zip->addFile($dir.$file);
                        }
                    }else{
                        // If directory
                        if(is_dir($dir.$file) ){

                        if($file != '' && $file != '.' && $file != '..'){

                            // Add empty directory
                            $zip->addEmptyDir($dir.$file);
                            $folder = $dir.$file.'/';
                            //print_r($folder);exit;
                            // Read data of the folder
                            $zip->addFile($dir.$file);
                        }
                        }

                    }
                    }
                    closedir($dh);
                }
            }
        }

0 个答案:

没有答案
相关问题