Create a Zip File in PHP

In this post you will learn how you can upload files and convert them into a zip format. We will use zip extension of php. This is included in php's default extensions bundle.

Create a Zip File in PHP
Files we are going to create for this task are index.php, process-zip.php and style.css. index.php will contain the form to upload files, process-zip.php will contain the code to create a zip file of uploaded files and style.css will handle the styling of index.php.

index.php

<!DOCTYPE html>
<html>
    <head>
        <title>Create a Zip File in PHP - Demo</title>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
        <link rel="stylesheet" href="css/style.css" />
    </head>
    <body>
        <div class="main-container">
            <div class="section">
                <form action="process-zip.php" method="POST" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-10">
                            <input type="file" class="file-input" name="zip_files[]" multiple="multiple"/>
                        </div>
                        <div class="col-2 text-right">
                            <button type="submit" class="btn btn-blue"><i class="fa fa-upload"></i> Upload</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </body>
</html>

process-zip.php

<?php
if(!empty($_FILES)){
    // If zip extension is not loaded then load it in runtime
    if(!extension_loaded("zip")){
        if(strtoupper(substr(PHP_OS, 0,3)) === "WIN"){
            dl("zip.dll");
        }else{
            dl("zip.so");
        }
    }
    //Initialize zip archive
    $zip = new ZipArchive();
    $name = time()."_zip_file.zip";
    $zip_file = __DIR__."/".$name;
    $allowed_files = ["pdf","jpg","gif","png","txt"];

    if($zip -> open($zip_file,ZIPARCHIVE::CREATE|ZIPARCHIVE::OVERWRITE)){
        // Loop all files and add them to zip
        for($i = 0; $i < count($_FILES['zip_files']['name']); $i++){
            $extension = pathinfo($_FILES['zip_files']['name'][$i],PATHINFO_EXTENSION);
            //If uploaded file type is in allowed filed types then add it to zip
            if(in_array($extension,$allowed_files)){
                $zip ->addFile($_FILES['zip_files']['tmp_name'][$i],$_FILES['zip_files']['name'][$i]);
            }
        }
        $zip -> close();
    }

    if(file_exists($zip_file)){
        // Code to download zip file
        ob_start();
        header("Content-Description: File Transfer");
        header("Content-Type: application/zip");
        header("Content-Disposition: attachment; filename=$name");
        header("Content-Length: " . filesize($zip_file));
        ob_clean();
        readfile($zip_file);
        unlink($zip_file);
    }
}
?>

style.css

*{
    box-sizing: border-box;
}
html,body{
    margin: 0px;
    padding: 0px;
}
body{
    background: #f0f0f0;
    font: normal normal 14px Open Sans,Verdana, Arial;
}
.main-container{
    max-width: 1024px;
    margin: 0px auto;
}
.section {
    padding: 15px;
}
.text-right{
    text-align: right;
}
.row:before,
.row:after{
    content: "";
    display: table;
}
.row:after{
    clear:both;
}
.row{
    margin-left: -10px;
    margin-right: -10px;
    margin-bottom: 10px;
}
[class*="col-"] {
    float: left;
    padding: 0px 10px;
}
.col-2{
    width: 16.66%;
}
.col-10{
    width: 83.33%;
}
.btn{
    display: inline-block;
    padding: 5px 10px;
    cursor: pointer;
}
.btn-blue{
    background: #3c8dbc;
    border: 1px solid #2b7cab;
    color: #fff;
}