Create a Zip File Using PHP and Download Multiple Files

In this example, i will explain how to create a zip file of multiple files using PHP and download. I will create a single file and also create a folder with name files where all files are stored.

Create a Zip File Using PHP

Download Complete Source Code

HTML:

<form name="zips" action="" method="post">
<input type="checkbox" name="files[]" value="SampleFile.pdf"/><label>PDF File</label><br />
<input type="checkbox" name="files[]" value="SampleFile.docx"/><label>Word File</label><br />
<input type="checkbox" name="files[]" value="HTML5.png"/><label>Image File</label><br />
<input type="submit" id="submit" name="createzip" value="Download All Seleted Files" >
</form>

PHP:

if(isset($_POST['files']))
{
$error = ""; //error holder
if(isset($_POST['createzip']))
{
$post = $_POST; 
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{ 
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{ 
// Checking files are selected
$zip = new ZipArchive(); // Load zip library 
$zip_name = time().".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['files'] 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";
}
}

Complete Webpage:

<?php
if(isset($_POST['files']))
{
$error = ""; //error holder
if(isset($_POST['createzip']))
{
$post = $_POST; 
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{ 
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{ 
// Checking files are selected
$zip = new ZipArchive(); // Load zip library 
$zip_name = time().".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['files'] 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";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create a Zip File Using PHP and Download Multiple Files</title>
</head>
<body>
<h1>Create a Zip File Using PHP and Download Multiple Files</h1>
<form name="zips" action="" method="post">
<input type="checkbox" name="files[]" value="SampleFile.pdf"/><label>PDF File</label><br />
<input type="checkbox" name="files[]" value="SampleFile.docx"/><label>Word File</label><br />
<input type="checkbox" name="files[]" value="HTML5.png"/><label>Image File</label><br />
<input type="submit" id="submit" name="createzip" value="Download All Seleted Files" >
</form>
</body>
</html>

Download Complete Source Code

Leave your comments if you find this tutorial helpful.

 

6 thoughts on “Create a Zip File Using PHP and Download Multiple Files

  1. thanks for your reply.

    I am also having trouble downloading files from specific locations. Can you tell me how to add a url to the value?? eg:

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s