使用ZipArchive类设置Zip存档的名称

时间:2018-03-16 14:01:34

标签: php

我目前拥有的代码(如下所示)成功将文件写入存档并启动文件下载,但文件名是随机的字母数字字符串。这些文件标题的一些例子是:

  • IfTO57y8.zip
  • PFuuPgMD.zip
  • KUgWDc0T.zip

如何将Zip文件夹的名称设置为更加用户友好的名称?

function start_brochures_download( $brochures ) {
$confirmation = "Your download should begin shortly. \r\n";
$error = "Files not set \r\n";

$files = $brochures;

if( $files ) {

    $zip = new ZipArchive();

    $current_time = time();

    $file_name = "Brochures-".$current_time;

    $file_folder = wp_upload_dir();
    $dir = $file_folder['path'];
    $zip_file = $dir.'/'.$file_name.'.zip';

    $zip->open( $zip_file, ZipArchive::CREATE );

    foreach ($files as $file) {
        if( !empty($file) ){
            # download file
            $download_file = file_get_contents($file);

            #add it to the zip
            $zip->addFromString(basename($file), $download_file);
        }
    }

    $zip->close();

    header('Content-disposition: attachment; filename="'.$zip_name.'"');
    header('Content-type: application/zip');
    header('Content-Length: ' . filesize($zip_file));
    // header("Location: $zip_file");
    readfile($zip_file);

} else {
    echo $error;
}
}

0 个答案:

没有答案