PHP - Simple File Archiver

In this tutorial we will create a Simple File Archiver using PHP. This code can compressed multiple files at the same time. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Getting started

First you have to download & install XAMPP or any local server that run PHP scripts. In my case I used XAMPP as my local server, here's the link for XAMPP server https://www.apachefriends.org/index.html.

The Main Function

This code contains the main function of the application. This code will try to compress all the files that have upload then create a zip file .To create this just write these block of code inside the text editor.
  1. <?php
  2.                         if(ISSET($_POST['submit'])){
  3.                                 if(array_sum($_FILES['upload']['error']) > 0){
  4.                                         echo "No Selected File";
  5.                                 }else{
  6.                                         $archive = new ZipArchive();
  7.                                         $archive->open("File.zip", ZipArchive::CREATE);
  8.                                         $files = $_FILES['upload'];
  9.                                        
  10.                                         for($i = 0; $i < count($files['name']); $i++){
  11.                                                 $tmp_name = $files['tmp_name'][$i];
  12.                                                 $filename = $files['name'][$i];
  13.                                                
  14.                                                 move_uploaded_file($tmp_name, $filename);
  15.                                                
  16.                                                 $archive->addFile("$filename");
  17.                                         }
  18.                                        
  19.                                         $archive->close();
  20.                                         echo "Successfully compressed the file";
  21.                                 }
  22.                         }
  23.                 ?>

The Main Interface

This is the interface that display the output of the application,to make this just write these codes inside your text editor, Then save it as index.php,
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple File Archiver</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-2"></div>
  18.                 <div class="col-md-8">
  19.                         <form method="POST" enctype="multipart/form-data">
  20.                                 <div class="form-inline">
  21.                                         <input class="form-control" type="file" name="upload[]" multiple/>
  22.                                         <button type="submit" class="btn btn-primary form-control" name="submit">Archive</button
  23.                                 </div>
  24.                         </form>
  25.                 </div>
  26.                 <br />
  27.         </div>
  28. </body>
  29. </html>
There you have it we successfully created a Simple File Archiver using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment