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.
  • 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="container">
<div class="my-4">
<form action="process-zip.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-6">
<input type="file" class="file-input" name="zip_files[]" multiple="multiple"/>
</div>
<div class="col-6 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

  • First check if there are any files uploaded.
  • Check if zip extension is loaded or not. If not loaded then load it based on operating system.
  • Initialize zip archive as $zip, name for the file as $filename, file path to store zip as $zip_file and array of extensions that are allowed to be added to zip. 
  • Loop through all files and if uploaded file's extension is present in allowed extensions array and file size is less than 2MB then add it to zip archive. 
  • Prepare the headers for download and download the zip archive file. Then delete it after triggering the download.
     
<?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'){
load_lib('zip.dll');
}else{
load_lib('zip.so');
}
}

// Initialize zip archive
$zip = new ZipArchive();
$filename = time() . '_zip_file.zip';
$zip_file = __DIR__ .'/'. $filename;
$allowed_files = ['pdf', 'jpg', 'jpeg', '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);
$file_size_kb = number_format($_FILES['zip_files']['size'][$i] / 1024, 2);

//If uploaded file type is in allowed filed types then add it to zip
if(in_array($extension,$allowed_files) && $file_size_kb < 2048 && $i <= 15){
$zip->addFile($_FILES['zip_files']['tmp_name'][$i],$_FILES['zip_files']['name'][$i]);
}
}
$zip->close();
}

// If file exists then set download headers
if(file_exists($zip_file)){
ob_start();

header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . $filename . '";');
header('Content-Length: ' . filesize($zip_file));

ob_clean();

readfile($zip_file);
unlink($zip_file);
}
}

style.css

*{
box-sizing: border-box;
}
html,body{
margin: 0;
padding: 0;
}
body{
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
line-height: 1.5;
}
.container{
width: 100%;
max-width: 1140px;
margin-right: auto;
margin-left: auto;
padding-right: 15px;
padding-left: 15px;
}
.my-4{
margin-top: 1rem;
margin-bottom: 1rem;
}
.text-right{
text-align: right;
}
.row{
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.col-6{
width: 100%;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
-webkit-box-flex: 0;
-ms-flex: 0 0 50%;
flex: 0 0 50%;
max-width: 50%;
}
.btn{
display: inline-block;
padding: 5px 10px;
cursor: pointer;
font: inherit;
}
.btn-blue{
background-color: #006699;
border: 1px solid #2b7cab;
color: #ffffff;
}