当文件名包含中文和日文内容时,Zip归档类无法压缩文件

时间:2014-10-29 10:52:23

标签: php ziparchive

我在这里提供我的代码,这里$ attchmntName以中文字符形式出现,但在附件名称上执行$ zip-> addFile函数后,在下载和解压缩附件时,带有中文和日文字符的文件名显示为特殊字符。

    <?php
    ob_start();
    include_once (__DIR__ . '/../../config.php');
    include_once('../emails/EmailObject/EmailAttachment.php');

    $filePath = $_POST['filePath'];
    $attchmntDtls = $_POST['attchmntDtls'];

    error_reporting(E_ALL);

    $zip = new ZipArchive();
    //$zipName = time().".zip"; // Zip name
    $zipName = "Email.zip";
    $zip->open($zipName,  ZipArchive::CREATE);
    foreach ($attchmntDtls as $attchmntDtl) {
        $path = $filePath."/".$attchmntDtl['fs_name'];
        if(file_exists($path)){
            //$zip->addFromString(basename($path),  file_get_contents($path));
            $attchmntName = $attchmntDtl['attachment_name'];
            $attchmntName = iconv_mime_decode($attchmntName,0, "UTF-8");
            //$attchmntName = htmlspecialchars($attchmntName);
            $zip->addFile($path, $attchmntName);
        } else {
            echo"file does not exist";
        }
    }

    $fileExists = $zip->filename;
    $zip->close();

    $objEmailAttachment = new EmailAttachment;
    $mimeType = $objEmailAttachment->get_mime_type($zipName);

    if(file_exists($fileExists)){
        $content = file_get_contents($fileExists);
        $length = strlen($content);

        header('Content-Description: File Transfer');
        header('Content-Type: '.$mimeType);`enter code here`
        header('Content-Disposition: attachment; filename="'.$zipName.'"');
        header('Content-Length: ' . $length);
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        header('Pragma: public');
        header('Set-Cookie: fileDownload=true; path=/');
        readfile($fileExists);

        ob_clean();
        flush();
        $statusOfMoving = rename($fileExists,$filePath."/".$zipName);
        if($length>0){
        echo $content;
        exit;
        }
    } else {
        exit();
    }

    ?>

0 个答案:

没有答案