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">
<head>
<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="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<style>

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

.sanf
{
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;

}
</style>
</head>
<body>


<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" >
</form>
</div>
</div>
</body>
</html>

<?php
$error = ""; //error holder

$post = $_GET;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// 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 ="Download.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['lang'] 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";

?>

Post a comment

0 Comments