Creating ZIP File with PHP

Creating ZIP File with PHP
PHP is great platform and provide many features. It provide list of classes which make our job easy. Among the list of classes it has class which provides extension to make zip of files. This extension enables you to transparently read or write ZIP compressed archives and the files inside them. This feature helps you while downloading multiple files in Zip compressed archives.

In this tutorial we will show you how to create and download files or multiple files in Zip compressed archives with Php. Below are the steps for "Creating ZIP File with PHP".

Step 1:- Create an index.php page with HTML code that contains list of files with input type checkbox name files[]. 
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="style.css" />
        <title>Download As Zip</title>
    </head>
    
    <body>
        <div class="main-wrapper">
            <center><h1 class="page_header">Create and Download Zip</h1></center>
            <form name="creat_zip" method="post">
                <?php if (!empty($error)) { ?>
                    <p class="error"><?php echo $error; ?></p>
                <?php } ?>
                <table class="table">
                    <tr>
                        <td><b>Select Files</b></td>
                        <td><b>File Type</b></td>
                        <td><b>File Name</b></td>
                    </tr>
                    
                    <tr>
                        <td align="center"><input type="checkbox" name="files[]" value="Chrysanthemum.jpg" /></td>
                        <td align="center"><img src="files/image.png" title="Image" /></td>
                        <td>Chrysanthemum.jpg</td>
                    </tr>

                    <tr>
                        <td align="center"><input type="checkbox" name="files[]" value="Desert.jpg" /></td>
                        <td align="center"><img src="files/image.png" title="Image" /></td>
                        <td>Desert.jpg</td>
                    </tr>

                    <tr>
                        <td align="center"><input type="checkbox" name="files[]" value="Hydrangeas.jpg" /></td>
                        <td align="center"><img src="files/image.png" title="Image" /></td>
                        <td>Hydrangeas.jpg</td>
                    </tr>

                    <tr>
                        <td colspan="3" align="center">
                            <input type="submit" name="download_zip" value="Download as ZIP" id="submit-btn" />
                            <input type="reset" name="reset" value="Reset" id="reset-btn"/>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>
Step 2:- Add PHP code in above fiel to covert the selected files into ZIP file format.
<?php
$error = ""; //error holder
if (isset($_POST['download_zip'])) {
    $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<br/>";
            }
            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 <br/>";
    }else
        $error .= "You dont have ZIP extension<br/>";
}
?>
Hope this php tutorial is useful for you. Keep following PHP Point for more Codes.

No comments:

Post a Comment