Hello Friend, hope you are doing good. Today i am going to share a simple php script which will upload your zip file to the web server and have it extract to your destination folder. It will saves your tons of time, think only of uploading. A useful php script to unzip the zip folder on your web server.
Basically it extracts the zip file into the directory you specify. Make sure the directory you want to extract it to has write permissions.
Include below CSS and JS files in your file before tags.
1 2 3 |
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<button data-toggle="modal" data-target="#myModal">Upload zip file</button> <div class="container"> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Select a zip file to upload</h4> </div> <div class="modal-body"> <form enctype="multipart/form-data" method="post"> <label><input type="file" name="zip_file" required="" /></label> <br /> <input type="submit" name="submit" class="modal-upload" value="Upload" /> </form> </div> </div> </div> </div> </div> |
Simple script to upload a zip file to the web server and have it unzipped to destination folder. It will Saves tons of time, think only of uploading. The function rmdir_recursive()
will extract .zip folder on your web server and after extract delete that .zip folder from your server using unlink("$dir/$file")
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?php /* * version : 1.0.0 * author : Prem Tiwari */ function rmdir_recursive($dir) { foreach(scandir($dir) as $file) { if ('.' === $file || '..' === $file) continue; if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file"); else unlink("$dir/$file"); } rmdir($dir); } if(isset($_FILES["zip_file"]["name"])) { $filename = $_FILES["zip_file"]["name"]; $source = $_FILES["zip_file"]["tmp_name"]; $type = $_FILES["zip_file"]["type"]; $name = explode(".", $filename); $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); foreach($accepted_types as $mime_type) { if($mime_type == $type) { $okay = true; break; } } $continue = strtolower($name[1]) == 'zip' ? true : false; if(!$continue) { $message = "The file you are trying to upload is not a .zip file. Please try again."; } /* PHP current path */ $path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in $filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase) $filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase) $targetdir = $path . $filenoext; // target directory $targetzip = $path . $filename; // target zip file /* create directory if not exists', otherwise overwrite */ /* target directory is same as filename without extension */ if (is_dir($targetdir)) rmdir_recursive ( $targetdir); mkdir($targetdir, 0777); /* Upload .zip folder */ if(move_uploaded_file($source, $targetzip)) { $zip = new ZipArchive(); $x = $zip->open($targetzip); // open the zip file to extract if ($x === true) { $zip->extractTo($targetdir); // place in the directory with same name $zip->close(); unlink($targetzip); } $message = "Your .zip file was uploaded and unpacked."; } else { $message = "There was a problem with the upload. Please try again."; } } ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: folder unzip php script, PHP, php unzip folders, unzip folder, unzip folder in php, unzip folder in php example, unzip folder on server using php, unzip folder using php, unzip folder using php script