How to create a zip file using PHP

前端 未结 6 1555
难免孤独
难免孤独 2020-11-29 02:40

I am trying to save files from one folder to another. zip folder placed in different directory. And I have written the fol

6条回答
  •  天命终不由人
    2020-11-29 03:00

    Please see this below example:

     0){    // Checking files are selected
                    $zip = new ZipArchive();            // Load zip library 
                    $zip_name = time().".zip";          // Zip name
                    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
                        $error .=  "* Sorry ZIP creation failed at this time
    "; } foreach($post['files'] as $file){ $zip->addFile($file_folder.$file); // Adding files into zip } $zip->close(); if(file_exists($zip_name)){ // push to download the zip header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="'.$zip_name.'"'); readfile($zip_name); // remove zip file is exists in temp path unlink($zip_name); } }else $error .= "* Please select file to zip
    "; }else $error .= "* You dont have ZIP extension
    "; } ?> Download As Zip

    Create Zip

    * File Type File Name
    a.jpg
    b.jpg
    c.docx
    d.pdf
     

提交回复
热议问题