joris

Upload and Unzip File

May 22nd, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2. if($_FILES["zip_file"]["name"]) {
  3.     $filename = $_FILES["zip_file"]["name"];
  4.     $source   = $_FILES["zip_file"]["tmp_name"];
  5.     $type     = $_FILES["zip_file"]["type"];
  6.  
  7.     $name = explode(".", $filename);
  8.     $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
  9.     foreach($accepted_types as $mime_type) {
  10.         if($mime_type == $type) {
  11.             $okay = true;
  12.             break;
  13.         }
  14.     }
  15.  
  16.     $continue = strtolower($name[1]) == 'zip' ? true : false;
  17.     if(!$continue) {
  18.         $message = "The file you are trying to upload is not a .zip file. Please try again.";
  19.     }
  20.  
  21.     $target_path = "/var/www/html/modules/about/".$filename;  // change this to the correct site path
  22.     if(move_uploaded_file($source, $target_path)) {
  23.         $zip = new ZipArchive();
  24.         $x = $zip->open($target_path);
  25.         if ($x === true) {
  26.             $zip->extractTo("/var/www/html/modules/about/"); // change this to the correct site path
  27.             $zip->close();
  28.  
  29.             unlink($target_path);
  30.         }
  31.         $message = "Your .zip file was uploaded and unpacked.";
  32.     } else {   
  33.         $message = "There was a problem with the upload. Please try again.";
  34.     }
  35. }
  36. ?>
  37. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  38. <html xmlns="http://www.w3.org/1999/xhtml">
  39. <head>
  40. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  41. <title>Untitled Document</title>
  42. </head>
  43.  
  44. <body>
  45. <?php if($message) echo "<p>$message</p>"; ?>
  46. <form enctype="multipart/form-data" method="post" action="">
  47. <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
  48. <br />
  49. <input type="submit" name="submit" value="Upload" />
  50. </form>
  51. </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment