jfs

Untitled

jfs
Oct 24th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. try {
  2.             if(empty($_FILES['uploaded_files'])){
  3.                 $files = '';
  4.             } else {
  5.                 //Attach multiple files one by one
  6.                 for ($i = 0; $i < count($_FILES['uploaded_files']['tmp_name']); $i++) {
  7.                     if ($i == $maximumfiles) {
  8.                         throw new RuntimeException('<div class="alert">Sie haben zuviele Dateien hochgeladen.</div>');
  9.                     }
  10.                     if (!isset($_FILES['uploaded_files']['error'][$i])) {
  11.                         throw new RuntimeException('<div class="alert">Etwas mit Ihren Dateien stimmt nicht.</div>');
  12.                     }
  13.                     switch ($_FILES['uploaded_files']['error'][$i]) {
  14.                         case UPLOAD_ERR_OK:
  15.                             break;
  16.                         // case UPLOAD_ERR_NO_FILE:
  17.                         //     throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint nicht verschickt worden zu sein.</div>');
  18.                         case UPLOAD_ERR_INI_SIZE:
  19.                         case UPLOAD_ERR_FORM_SIZE:
  20.                             throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint von der Datenmenge groß zu sein.</div>');
  21.                         // default:
  22.                         //     throw new RuntimeException('<div class="alert">Ein unbekannter Fehler ist aufgetreten.</div>');
  23.                     }
  24.                     if ($_FILES['uploaded_files']['size'][$i] > 5000000) {
  25.                         throw new RuntimeException('<div class="alert">Die maximale Dateigröße ist überschritten.</div>');
  26.                     }
  27.  
  28.                     $extension = strtolower(pathinfo($_FILES['uploaded_files']['name'][$i], PATHINFO_EXTENSION));
  29.                     $allowed_extensions = array('jpg', 'jpeg', 'pdf');
  30.  
  31.                     if(!in_array($extension, $allowed_extensions)) {
  32.                         throw new RuntimeException('<div class="alert">Nicht erlaubtes Dateiformat!</div>');
  33.                     }
  34.  
  35.                     $allowed_types = array ('application/pdf', 'image/jpeg');
  36.                     $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
  37.                     $detected_type = finfo_file( $fileInfo, $_FILES['uploaded_files']['tmp_name'][$i] );
  38.                     if ( !in_array($detected_type, $allowed_types) ) {
  39.                         throw new RuntimeException('<div class="alert">Nicht erlaubtes Dateiformat!</div>');
  40.                     }
  41.                     finfo_close( $fileInfo );
  42.  
  43.                     $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['uploaded_files']['name'][$i]));
  44.                     $filename = $_FILES['uploaded_files']['name'][$i];
  45.  
  46.                     if (move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$i], $uploadfile)) {
  47.                         $files = $mail->addAttachment($uploadfile, $filename);
  48.                     } else {
  49.                         $msg .= 'Failed to move file to ' . $uploadfile;
  50.                     }
  51.                 }
  52.             }
Advertisement
Add Comment
Please, Sign In to add comment