Time Ago PHP Function

 

PHP Time Ago Function is moved here: http://www.allphptricks.com/php-time-ago-function/

Advertisements

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.

 

Enable Disable Submit Button If One Checkbox is Checked

This is very simple tutorial which enable submit button if at least one checkbox is checked otherwise disable submit button.

Enable Disable Submit Button

Note: Don’t forget to include the jQuery library in the header or footer.

Demo: https://jsfiddle.net/javedfiddle/fj8r9w9v/

HTML:

<form method="post" action="">
<input class="chk" type="checkbox" name="html" value="html" />HTML<br/>
<input class="chk" type="checkbox" name="javascript" value="javascript" />JavaScript<br/>
<input class="chk" type="checkbox" name="jquery" value="jquery" />JQuery<br/><br/>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>

JavaScript:

$('#submit').prop("disabled", true);
$('input:checkbox').click(function() {
 if ($(this).is(':checked')) {
 $('#submit').prop("disabled", false);
 } else {
 if ($('.chk').filter(':checked').length < 1){
 $('#submit').attr('disabled',true);}
 }
});

Demo: https://jsfiddle.net/javedfiddle/fj8r9w9v/

Leave your comments if you find this tutorial helpful.