SHARE
TWEET

CPW Uploads 2014 v1.1 | Multiple PHP File Upload

tunnckoCore Jan 11th, 2014 426 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Multiple PHP File Upload v1.1, 05 Feb. 2014
  4.  * Super-easy config through form hidden fields!
  5.  *
  6.  * @author      George Yanev (http://github.com/tunnckoCore)
  7.  *
  8.  * @license     MIT License (http://opensource.org/licenses/MIT)
  9.  * @copyright   2014, WAF Development
  10.  */
  11. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  12.     $cntFILES = count($_FILES['cpw_whistle']['name']);
  13.     $cntPOST = count($_POST);
  14.  
  15.     if ($cntFILES > 0 && $cntPOST > 1) {
  16.         //echo "<pre>" . print_r($_POST, true) . "</pre>";
  17.         //echo "<pre>" . print_r($_FILES, true) . "</pre>";
  18.  
  19.         for ($cpw = 0; $cpw < $cntFILES; $cpw++) {
  20.  
  21.             //leka normalizaciq
  22.             $_basename = strtolower($_FILES['cpw_whistle']['name'][$cpw]);
  23.             $_tempname = $_FILES['cpw_whistle']['tmp_name'][$cpw];
  24.             $_errorNum = (int) $_FILES['cpw_whistle']['error'][$cpw];
  25.             $_filesize = (int) $_FILES['cpw_whistle']['size'][$cpw];
  26.             $_fileMime = $_FILES['cpw_whistle']['type'][$cpw];
  27.  
  28.             // podrejdame sobstven po-udoben masiv
  29.             $_APIF['cpw_whistle'][$cpw]['tmp_name'] = $_tempname;
  30.             $_APIF['cpw_whistle'][$cpw]['error'] = $_errorNum;
  31.             $_APIF['cpw_whistle'][$cpw]['name'] = $_basename;
  32.             $_APIF['cpw_whistle'][$cpw]['size'] = $_filesize;
  33.             $_APIF['cpw_whistle'][$cpw]['type'] = $_fileMime;
  34.  
  35.             //ako ima nqkakfa PHP/serverna greshka
  36.             //izobshto nqma smisal da produljavame
  37.             if ($_APIF['cpw_whistle'][$cpw]['error'] == 0) {
  38.  
  39.                 list($endFilename, $imageExtension) = explode(".", $_APIF['cpw_whistle'][$cpw]['name']);
  40.  
  41.                 //vzimame pozvolenite razshireniq
  42.                 $allowedExtensions = explode(",", $_POST['cpw_allow_extensions']);
  43.  
  44.  
  45.                 // ako E napisano novo ime polzva nego
  46.                 // ako NE polzva imeto na faila koito se ka4va
  47.                 if (!empty($_POST['cpw_new_filename'])) {
  48.                     $endFilename = strip_tags(trim($_POST['cpw_new_filename']));
  49.                 }
  50.                 //postroqvame cqloto ime na ka4vashtiqt se fail
  51.                 $finalImageName = $endFilename . '.' . $imageExtension;
  52.  
  53.  
  54.                 //proverqvame dali failat e s nqkoe ot pozvolenite razshireniq
  55.                 //ako ne e, zapisvame string v masiva errors
  56.                 if (!in_array($imageExtension, $allowedExtensions)) {
  57.                     $response['errors'][$cpw]['filename'] = $finalImageName;
  58.                     $response['errors'][$cpw]['message'] = 'Това (' . $imageExtension . ') файлово разширение не е позволено.';
  59.                     $response['errors'][$cpw]['key'] = 'not_allowed_extension';
  60.                     $response['errors'][$cpw]['code'] = 40001;
  61.                 }
  62.  
  63.                 //proverqvame dali goleminata na faila e po-golqma ot
  64.                 //poso4eniq maximum, ako e zapisvame greshka
  65.                 if ($_APIF['cpw_whistle'][$cpw]['size'] > ($_POST['cpw_max_singlefile_size'] * 1024 * 1024)) {
  66.                     $response['errors'][$cpw]['filename'] = $finalImageName;
  67.                     $response['errors'][$cpw]['message'] = 'Размера на ' . $finalImageName . ' e по-голям от определеният максимум.';
  68.                     $response['errors'][$cpw]['key'] = 'max_singlefile_size';
  69.                     $response['errors'][$cpw]['code'] = 40002;
  70.                 }
  71.                 //ako nqma greshki dotuk produljavame sus suzdavaneto
  72.                 //na direktoriqta poso4ena za upload i
  73.                 //opravqneto na neinite prava
  74.                 if (!isset($response['errors'][$cpw])) {
  75.  
  76.                     //suzdavame putq do upload papkata
  77.                     $uploadDirectory = trim($_POST['cpw_uploadpath'], '/');
  78.                     $uploadPath = __DIR__ . DIRECTORY_SEPARATOR . $uploadDirectory;
  79.  
  80.                     $finalPath = realpath($uploadPath);
  81.  
  82.                     //proverqvame dali direktoriqta sushtestvuva
  83.                     //ako ne sushtestvuva, suzdavame q
  84.                     if (!$finalPath) {
  85.                         mkdir($uploadPath);
  86.                         chmod($uploadPath, 0777);
  87.                         $finalPath = $uploadPath;
  88.                     }
  89.  
  90.  
  91.                     if (move_uploaded_file($_APIF['cpw_whistle'][$cpw]['tmp_name'], $finalPath . DIRECTORY_SEPARATOR . $finalImageName)) {
  92.                         $response['success'][$cpw]['filename'] = $finalImageName;
  93.                         $response['success'][$cpw]['message'] = $uploadDirectory . '/' . $finalImageName;
  94.                         $response['success'][$cpw]['key'] = 'success_upload';
  95.                         $response['success'][$cpw]['code'] = 44444;
  96.                     } else {
  97.                         $response['errors'][$cpw]['filename'] = $finalImageName;
  98.                         $response['errors'][$cpw]['message'] = 'Грешка при move_uploaded_file().';
  99.                         $response['errors'][$cpw]['key'] = 'error_uploading_file';
  100.                         $response['errors'][$cpw]['code'] = 40003;
  101.                     }
  102.                 }
  103.             } else {
  104.                 $response['errors'][$cpw]['filename'] = $finalImageName;
  105.                 $response['errors'][$cpw]['message'] = 'Възникнала е някаква сървърна грешка.'
  106.                         . 'Моля проверете стойностите на upload_max_filesize, '
  107.                         . 'post_max_size или max_file_uploads в използваният php.ini';
  108.                 $response['errors'][$cpw]['key'] = 'general_server_error';
  109.                 $response['errors'][$cpw]['code'] = 50000;
  110.             }
  111.         }
  112.     }
  113. }
  114. echo '<ul>';
  115. if (isset($response['errors'])) {
  116.     $response['errors'] = array_values($response['errors']);
  117.    
  118.     for ($i = 0; $i < count($response['errors']); $i++) {
  119.         $data = '<li><b>Filename:</b> ' . $response['errors'][$i]['filename'] . ' | ';
  120.         $data .= '<b>Keycode:</b> ' . $response['errors'][$i]['key'] . ' | ';
  121.         $data .= '<b>Message:</b> ' . $response['errors'][$i]['message'] . '</li>';
  122.         echo '<div style="color: red;">'.$data.'</div>';
  123.     }
  124. }
  125. if (isset($response['success'])) {
  126.     $response['success'] = array_values($response['success']);
  127.    
  128.     for ($i = 0; $i < count($response['success']); $i++) {
  129.         $data = '<li><b>Filename:</b> '.$response['success'][$i]['filename'].' | ';
  130.         $data .= '<b>Path2image:</b> '.$response['success'][$i]['message'].'</li>';
  131.         echo '<div style="color: green;">'.$data.'</div>';
  132.     }
  133. }
  134. echo '</ul>';
  135.  
  136.  
  137. /**
  138.  * Za pove4e informaciq ot otgovora:
  139.  * echo '<pre>' . print_r($response, true) . '</pre>';
  140.  */
  141.  
  142. ?>
  143. <form action="" method="post" enctype="multipart/form-data">
  144.     <input type="text" name="cpw_new_filename" placeholder="New filename">
  145.     <input type="hidden" name="cpw_uploadpath" value="simpleupload-files">
  146.     <input type="hidden" name="cpw_max_singlefile_size" value="1">
  147.     <input type="hidden" name="cpw_allow_extensions" value="png,jpg,jpeg,gif">
  148.     <input type="file" name="cpw_whistle[]" required multiple accept="image/*">
  149.     <button type="submit">Прикачи</button>
  150. </form>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top