Posted By

jvandemerwe on 02/15/13


Tagged

php wordpress server upload zip joomla unzip


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

maidul
balmeet80


Script for uploading ZIP file and unzip it on the server


 / Published in: PHP
 

Easy PHP script for uploading and unpacking zip files to the server much faster

Steps to follow:

  1. create folder on the webserver called "zipper" or something else
  2. copy the following PHP code into a file called "zipper.php" into this folder
  3. use the url to this file to start the little script
  4. select a zip file from your harddisk and go for it
  5. after upload a directory exists in the folder "zipper" from step 1

with ftp program (like Filezilla) you can easily move the content to the directory of your choice (that's all), which goes much faster

  1. <?php
  2. /* Simple script to upload a zip file to the webserver and have it unzipped
  3.   Saves tons of time, think only of uploading Wordpress to the server
  4.   Thanks to c.bavota (www.bavotasan.com)
  5.   I have modified the script a little to make it more convenient
  6.   Modified by: Johan van de Merwe (12.02.2013)
  7. */
  8.  
  9. function rmdir_recursive($dir) {
  10. foreach(scandir($dir) as $file) {
  11. if ('.' === $file || '..' === $file) continue;
  12. if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
  13. else unlink("$dir/$file");
  14. }
  15.  
  16. rmdir($dir);
  17. }
  18.  
  19. if($_FILES["zip_file"]["name"]) {
  20. $filename = $_FILES["zip_file"]["name"];
  21. $source = $_FILES["zip_file"]["tmp_name"];
  22. $type = $_FILES["zip_file"]["type"];
  23.  
  24. $name = explode(".", $filename);
  25. $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
  26. foreach($accepted_types as $mime_type) {
  27. if($mime_type == $type) {
  28. $okay = true;
  29. break;
  30. }
  31. }
  32.  
  33. $continue = strtolower($name[1]) == 'zip' ? true : false;
  34. if(!$continue) {
  35. $message = "The file you are trying to upload is not a .zip file. Please try again.";
  36. }
  37.  
  38. /* PHP current path */
  39. $path = dirname(__FILE__).'/'; // absolute path to the directory where zipper.php is in
  40. $filenoext = basename ($filename, '.zip'); // absolute path to the directory where zipper.php is in (lowercase)
  41. $filenoext = basename ($filenoext, '.ZIP'); // absolute path to the directory where zipper.php is in (when uppercase)
  42.  
  43. $targetdir = $path . $filenoext; // target directory
  44. $targetzip = $path . $filename; // target zip file
  45.  
  46. /* create directory if not exists', otherwise overwrite */
  47. /* target directory is same as filename without extension */
  48.  
  49. if (is_dir($targetdir)) rmdir_recursive ( $targetdir);
  50.  
  51.  
  52. mkdir($targetdir, 0777);
  53.  
  54.  
  55. /* here it is really happening */
  56.  
  57. if(move_uploaded_file($source, $targetzip)) {
  58. $zip = new ZipArchive();
  59. $x = $zip->open($targetzip); // open the zip file to extract
  60. if ($x === true) {
  61. $zip->extractTo($targetdir); // place in the directory with same name
  62. $zip->close();
  63.  
  64. unlink($targetzip);
  65. }
  66. $message = "Your .zip file was uploaded and unpacked.";
  67. } else {
  68. $message = "There was a problem with the upload. Please try again.";
  69. }
  70. }
  71.  
  72.  
  73. ?>
  74. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  75. <html xmlns="http://www.w3.org/1999/xhtml">
  76. <head>
  77. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  78. <title>Unzip a zip file to the webserver</title>
  79. </head>
  80.  
  81. <body>
  82. <?php if($message) echo "<p>$message</p>"; ?>
  83. <form enctype="multipart/form-data" method="post" action="">
  84. <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
  85. <br />
  86. <input type="submit" name="submit" value="Upload" />
  87. </form>
  88. </body>
  89. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: scade on October 31, 2014

Worked perfectly, thank you so much !

You need to login to post a comment.