ToKeiChun

Auto Extract File Zip [Upload ZIP > Auto Extract]

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