Download selected file in zip format using php form

Hi friends now we will see how to download user selected files from php form in zip file format. In this example I used 3 different dummy files for download, format of this files are file1 php format, file2 doc format, file3 image format. This files I saved in the folder name called as a files. I stored all this option files in the array name called as a lang[]. I used get method this php form to get the user selected files. I stored the zip file in temp path.

I provided full code here you can modify this in your way. Visit the live demo then you will get an idea how it is working. You can download the files by clicking download button below. I hope this Download selected file in zip format using php form example will help you.

Code for download zip file format for selected file:

<!DOCTYPE html>
<html lang="en">
<title>Download selected file in zip format using php form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>
<script src=""></script>

.container {
max-width: 500px;
margin: 100px auto;
display: block;
background-color: #ffc107;
padding: 40px;
border: 1px solid #cc9b09;
border-radius: 22px;

margin-right: 40px;
font-size: 18px;

[type=button]:not(:disabled), [type=reset]:not(:disabled), [type=submit]:not(:disabled), button:not(:disabled)
background-color: #b78d0f;
border: 0px;
padding: 10px 25px;
margin: 18px auto;
width: 250px;
display: block;
color: #fff;
font-weight: bold;


<div class="container">
<!-- Control the column width, and how they should appear on different devices -->
<form method="GET" action=""  class="form-group">
<div class="form-group"> <input type="text" placeholder="Enter your name" class="form-control"  /></div><br/>
<div class="checkbox">
<p><span> <strong>Select Your Files</strong></span></p>
<span class="sanf"><input type="checkbox" name='lang[]' value="file1.php" > File1 </span>
<span class="sanf"><input type="checkbox" name='lang[]' value="file2.docx"> File2 </span>
<span class="sanf"><input type="checkbox" name='lang[]' value="file3.png" > File3  </span><br/><br/>
<input type="submit" value="Submit" name="submit" >

$error = ""; //error holder

$post = $_GET;
$file_folder = "files/"; // folder to load files
// Checking ZIP extension is available
if(isset($post['lang']) and count($post['lang']) > 0)
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name =""; // 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['lang'] as $file)
$zip->addFile($file_folder.$file); // Adding files into zip


// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
// remove zip file is exists in temp path

$error .= "* Please select file to zip ";
$error .= "* You dont have ZIP extension";


Post a Comment