0xspade

Multiple FIle Uploads Script [Secured]

Jan 29th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <!Doctype html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>Multiple File Uploads</title>
  6.     </head>
  7.     <body>
  8.         <form action="" method="POST" enctype="multipart/form-data">
  9.             <input type="file" name="files[]" multiple />
  10.             <input type="submit" value="Upload File">
  11.         </form>
  12.     </body>
  13. </html>
  14. <?
  15.     if(!empty($_FILES['files']['name'][0])){
  16.         $files = $_FILES['files'];
  17.  
  18.         $uploaded = array();
  19.         $failed = array();
  20.  
  21.         $allowed = array('jpg', 'png', 'gif', 'jpeg');
  22.  
  23.         foreach($files['name'] as $position => $filename){
  24.             $file_tmp = $files[tmp_name][$position];
  25.             $file_size = $files['size'][$position];
  26.             $file_error = $files['error'][$position];
  27.  
  28.             $file_ext = explode('.', $filename);
  29.             $file_ext = strtolower(end($file_ext));
  30.             if(in_array($file_ext, $allowed)){
  31.                 if($file_error === 0){
  32.                     if($file_size <= 2097152){
  33.                         $new_file = md5(uniqid('', true)).'.'.$file_ext;
  34.                         $file_destination = "uploads/".$new_file;
  35.  
  36.                         if(move_uploaded_file($file_tmp, $file_destination)){
  37.                             $uploaded[$position] = $file_destination;
  38.                         }else{
  39.                             $failed[$position] = "[{$filename}] failed to upload!";
  40.                         }//move uploaded files
  41.                     }else{
  42.                         $failed[$position] = "[{$filename}] is too large!";
  43.                     }//file size
  44.                 }else{
  45.                     $failed[$position] = "[{$filename}] errored with code {$file_error}!";
  46.                 }//file error
  47.             }else{
  48.                 $failed[$position] = "[{$filename}] file extension '{$file_ext}' is not allowed!";
  49.             }//in_array
  50.         }// foreach ends here
  51.  
  52.         if(!empty($uploaded)){
  53.             print_r($uploaded);
  54.         }
  55.  
  56.         if(!empty($failed)){
  57.             print_r($failed);
  58.         }
  59.     }
  60.  
  61. ?>
Add Comment
Please, Sign In to add comment