Advertisement
michaelyuen

Untitled

Apr 25th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. <?php
  2.     ini_set('display_errors',1); // enable php error display for easy trouble shooting
  3.     error_reporting(E_ALL); // set error display to all
  4.     DEFINE ('_BASEFOLDER_', 'folder');
  5.     DEFINE ('_TMPFOLDER_','folder/_tmp');
  6.    
  7.     function clearTmpFolder() {
  8.         if (!file_exists(_TMPFOLDER_)) {
  9.             mkdir(_TMPFOLDER_);
  10.         } else {
  11.             foreach (GLOB(_TMPFOLDER_ . '/*') as $files) {
  12.                 if (!unlink($files)) {
  13.                 die('Unable to clear tmp folder. Please check folder permission');
  14.                 }
  15.             }
  16.         }
  17.     }
  18.    
  19.     if (isset($_POST['folder'])) {
  20.         if (!file_exists(_BASEFOLDER_)) {
  21.             mkdir(_BASEFOLDER_);
  22.         }
  23.         if (isset($_FILES['project'])) {
  24.             $file_path = _TMPFOLDER_ . '/temp.zip';
  25.             clearTmpFolder();
  26.             if (move_uploaded_file($_FILES['project']['tmp_name'], $file_path)) {
  27.                 $zip = new ZipArchive;
  28.                 if ($zip->open($file_path) === TRUE) {
  29.                     $newfolder = 'folder/' . $_POST['folder'] . '_'. microtime();
  30.                     mkdir($newfolder);
  31.                     $zip->extractTo($newfolder);
  32.                     $zip->close();
  33.                     clearTmpFolder();
  34.                 } else {
  35.                     echo 'Fail to open zip file. File may be corrupted';
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41. ?>
  42.  
  43. <form action="" method="POST" enctype="multipart/form-data">
  44.     <input type="file" name="project" accept=".zip">
  45.     <input type="hidden" name="folder" value="project">
  46.     <input type="submit" name="submit" value="submit">
  47. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement