Zip文件夹,然后允许用户下载


Zip folder first then allow user to download it

在我的网站上,我想要一个链接,可以压缩包含其内容的特定文件夹,然后允许用户下载。它将用于下载捆绑文件。

试试这个代码:

 $files[] = base_url().'uploads/'.$names->file_name;

将文件传递到此$files[]数组中。

$zip = new ZipArchive();  
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);   
    foreach($files as $file)
    {      //echo $file; exit;
        $download_file = file_get_contents($file); 
        $zip->addFromString(basename($file),$download_file); 

    }
    $zip->close();
    header('Content-disposition: attachment; filename=project_files.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);