Download selected file in zip format using php form | Sanwebcorner

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

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap Bootstrap form validation border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed donation form download zip email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient heart animation hide menu Horizontal News ticker Hover Htm Html html dropdown html form html form validation html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript javascript validation Jquery jquery validation js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form multi-select-checkbox Multi-Step-form Multiple File Upload in PHP mysql News ticker on hover change text to image our team page password generator password strength checker paypal form paypal html form Photoshop PHP php mailer populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips whatsapp link website Whislist Youtube