Loading...
 
Gmail / Facebook Style Chat Application


Posted: Saturday 29th of September 2012

Create ZIP Files with PHP



This tutorial will help as many who have been having problems creating zip files for their businesses and other needs.
This program is completely written in PHP to create a zip file from any kind of document.
The script will display the content of a given directory on the screen with an option to select your desired file or directory to zip and as well displays all the zipped files on the screen.
It also detects if an item is a directory or a file and much more...




Download Live Demo


Code:

<?php
// increase script execution time if necessary
ini_set("max_execution_time", 10000);

if($_POST["zip_file_now"]=="1")
{
$File_you_want_to_zip = $_POST["fileToZip"];

if($File_you_want_to_zip == "")
{
$zipped_file_creation_status = '<br><font style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;color:red;">Please select a file above to zip.</font>';
}
else
{
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source))
{
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE))
{
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', realpath($file));

if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}

//if zipped successfully, return success message otherwise, return failure message
if(Zip($File_you_want_to_zip,'zipped files/'.date("d-m-Y").'_'.rand(123456,098765).'.zip'))
{
$zipped_file_creation_status = '<br><font style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;color:blue;">Zip file created successfully.</font>';
}
else
{
$zipped_file_creation_status = '<br><font style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;color:red;">Zip file creation was unsuccessful.</font>';
}
}
}

?>

<center>
<div id="vasp" style="">Create ZIP Files with PHP</div><br clear="all" /><br clear="all" />
<div style="width:600px;border:6px solid #F6F6F6; padding:10px;">
<div style=" float:left;width:250px; border:0px solid black; font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; line-height:22px;" align="left">
<b>ZIPPED FILES ARE SHOWN BELOW</b>
<?php

//Create a handler to read the directory contents
$handler = opendir("zipped files/");
$found = FALSE; // Used to see if there were any valid files
//Keep going until all files in directory have been read
while ($file = readdir($handler))
{
if (preg_match ("/.zip$/i", $file))
{
echo '<p align="left"><a href="zipped files/' . $file . '">' . $file . '</a></p>';
$found = true;
}
}
closedir($handler);
if ($found == FALSE) { echo "<br><p align='left'>No zipped files at the moment</p><br>"; }
else { }
?>
</div>
<form method="post" action="zip.php">
<div style=" float:left;width:250px;">
<?php
if($open_directory_to_read_its_content = opendir("."))
{
echo '<select name="fileToZip" id="fileToZip" style=" width:250px;font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;line-height:25px;padding:3px;">';
echo '<option value="">Please select a file to zip</option>';
while(($directory_contents = readdir($open_directory_to_read_its_content)) !== false)
{
if (preg_match ("/.zip$/i", $directory_contents))
{
//Don't show zipped files in this area
}
elseif(is_dir($directory_contents) && $directory_contents != "." && $directory_contents != "..")
{
echo '<option value="'.$directory_contents.'">'.$directory_contents.' is a directory</option>';
}
elseif(is_file($directory_contents))
{
echo '<option value="'.$directory_contents.'">'.$directory_contents.' is a file</option>';
}
}
closedir($open_directory_to_read_its_content);
}
echo '</select>'.$zipped_file_creation_status;
?>

</div>
<div style=" float:left;width:100px;">
<input type="submit" name="Submit" value="Submit" style="padding:3px; width:96px; cursor:pointer;" />
<input type="hidden" id="zip_file_now" name="zip_file_now" value="1" />
</div><br clear="all" />
</form>
</div><br clear="all" />
</center>




That's it guys...


Views Today: 0
Total Views: 950

Comments
0
OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Copyright © 2011 - 2024 | All Rights Reserved