• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Programming

How to create and download a Zip file with PHP

Nov 6, 2021 Editorial Staff Leave a Comment

Share
Tweet
Share

Today we discuss how to create a zip file from a folder of images and download it from the browser. As result, we will have all the files in a folder archived to a zip file and downloaded to our computer.

Important to realize we use built-in PHP class ZipArchive to achieve this. ZipArchive Class which allows us to create a Zip file. This class makes file creation easier.

Contents

    1. HTML Form & PHP
    2. Bootstrap
    3. Download

HTML & PHP

As with other tutorials on the website we use Twitter bootstrap to create scaffolding for the little application, which is easier and understandable for all people.

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <div class="col-5 mx-auto">
        <h1>Download Zip File from a folder</h1>

        <h3>Files in the folders</h3>

        <?php $files = scandir('./images');

        foreach($files as $file) {
            if($file == '.' || $file == '..') continue;
            print "<img src='./images/".$file."' width='100' class='mb-4'>";
            print "<br>";
        }

        ?>

        <form action="download.php" method="post">
            <button type="submit" class="btn btn-info">Download Zip</button>
        </form>
    </div>    

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    
  </body>
</html>

Create and download the Zip file

When the beautiful form submits, it triggers the download.php file and as a first step, it creates the zip file, and then with the help of PHP force download headers, it will download the file to your computer.

<?php
$zipFile = "images.zip";

// Initializing PHP class
$zip = new ZipArchive();
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);

$files = scandir('./images');

foreach($files as $file) {
    if($file == '.' || $file == '..') continue;
    $zip->addFile('./images/'.$file, $file);
}

$zip->close();

//Force download a file in php
if (file_exists($zipFile)) {
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="' . basename($zipFile) . '"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate');
	header('Pragma: public');
	header('Content-Length: ' . filesize($zipFile));
	readfile($zipFile);
	exit;
}

?>

Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required



Search Here

Share

   



Hot topics

  • Replace “\n” with new line characters, using Notepad++ 15 views
  • Solution: windows photo viewer opens each time save a pic file 12 views
  • A Simple YouTube Video Downloader Script in PHP 10 views
  • How to Create PDF File From HTML Form Using Fpdf? 9 views
  • Simple PHP Shopping Cart 8 views
  • All Countries SQL file with Country Codes to Download 8 views
  • How to create and download a Zip file with PHP 8 views
  • How to set up GitHub SSH in Ubuntu with example 7 views
  • How to enter new line in Microsoft Teams? 7 views
  • Hierarchical Tree view Category Example in Laravel 7 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube























Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com