dongsou3041 2016-11-22 00:35
浏览 76

在php中压缩文件时存档未知或损坏错误

I'm trying to write code to zip a file and download it. My problem is when I download and open the file, I get the error

The archive is either in unknown format or damaged

Here's my code:

<?php
try{
$db_conn = new DBController();
$db_conn->prepare("SELECT filetypename FROM dirlistening WHERE dadroot = :BasName AND sonbas = :BasDirName");
$db_conn->bind(':BasName', $parentBasIndex);
$db_conn->bind(':BasDirName', $currentBas);
$db_conn->execute();
$dbzipoutput = $db_conn->getAll();
$db_conn->free();
$files = array();
    if(!is_null($dbzipoutput)){
        foreach($dbzipoutput as $azip){
            $files[] = $azip->filetypename;
        }
    }
//$file_names = array("'" . implode( $filess, "','" ) . "'");
$file_names = $files;
    function zipSourceFilesDownload($file_names,$archive_file_name,$file_path){
          $zip = new ZipArchive();
        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE){
            exit("cannot open ".$archive_file_name."
");
        }else{
            foreach($file_names as $filelist){
                $zip->addFile($file_path.'/'.$filelist);
                $zip->addEmptyDir($file_path.'/'.$filelist);
                $zip->addFromString($file_path.'/'.$filelist, file_get_contents($file_path.'/'.$filelist));
                $zip->setArchiveComment("zipped on ".date('Y-M-d')."
                    
Downloaded from: My website 
                    
Website: http://www.example.com
                    
Project Source: ".$file_path."
                    
Project Url: http://example.com" . $_SERVER['REQUEST_URI']."
                    
FILE COMMENT AND DESCRIPTION
                    
 none");  
            }
            $var = $zip->close();
            $download_file = true;
        }

        if($download_file){
            ob_get_clean();
            header("Pragma: no-cache");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private", false);
            header("Content-Type: application/zip");
            header('Content-disposition: attachment; filename='.$archive_file_name);
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: " . filesize($archive_file_name));
            print $zip_file;
            unlink($archive_name);
        }
        return $var;        
    }
    zipSourceFilesDownload($file_names,$archive_file_name,$file_path);  
}catch (PDOException $e){ echo 'Connection failed: ' . $e->getMessage();} 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?