PHP - Simple Extract Zip Image Files Using Ajax
Submitted by razormist on Thursday, June 28, 2018 - 20:52.
In this tutorial we will create Simple Extract Zip Image Files Using Ajax. 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.
There you have it we successfully created Simple Extract Zip Image Files Using Ajax. 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!!!
Before we get started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <form method="POST" enctype="multipart/form-data">
- <label>Please Select Zip File</label>
- <br />
- <input id="zip_file" type="file" name="zip_file"/>
- <br />
- <button type="button" id="btn_zip" name="btn_zip" class="btn btn-primary">Upload</button>
- </form>
- <div id="result"></div>
- </div>
- </body>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/script.js"> </script>
- </html>
Creating PHP Query
This code contains the php query of the application.This code will extract the zip file with the ajax request. To do that just copy and write this block of codes inside the text editor, then save it as extract.php.- <?php
- if($_FILES['zip_file']['name'] != ''){
- $output = "";
- $file_name = $_FILES['zip_file']['name'];
- $name = $array[0];
- $ext = $array[1];
- if($ext == 'zip'){
- $path = 'upload/';
- $location = $path . $file_name;
- $zip = new ZipArchive;
- if($zip->open($location)){
- $zip->extractTo($path);
- $zip->close();
- }
- foreach($files as $file){
- $new_file = $tmp_ext[0].".".$file_ext;
- $output .= "<br />".$new_file."<center><img src='upload/".$name."/".$new_file."' height='50px' width='50px'/></center>";
- }
- }
- }
- }
- echo $output;
- }
- ?>
Creating The Ajax Script
This is where the code that uses ajax request been used. This code will sent ajax request to the php script to process the zip file. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.- <script>
- $(document).ready(function(){
- $('#btn_zip').on('click', function(){
- var file_data = $('#zip_file').prop('files')[0];
- if(file_data != undefined){
- var form_data = new FormData();
- form_data.append('zip_file', file_data);
- $.ajax({
- type: 'POST',
- url: 'extract.php',
- contentType: false,
- processData: false,
- data: form_data,
- success: function(data){
- $('#result').html(data);
- }
- });
- }
- return false;
- });
- });
- </script>
Add new comment
- 252 views