Advertisement
metalx1000

HTML5 Upload Multiple Files

Jan 24th, 2017
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['submit'])){
  3.   if(count($_FILES['upload']['name']) > 0){
  4.     //Loop through each file
  5.     for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  6.       //Get the temp file path
  7.       $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
  8.       //Make sure we have a filepath
  9.       if($tmpFilePath != ""){
  10.  
  11.         //save the filename
  12.         $shortname = $_FILES['upload']['name'][$i];
  13.  
  14.         //save the url and the file
  15.         $filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
  16.  
  17.         //Upload the file into the temp dir
  18.         if(move_uploaded_file($tmpFilePath, $filePath)) {
  19.           $files[] = $shortname;
  20.           //insert into db
  21.           //use $shortname for the filename
  22.           //use $filePath for the relative url to the file
  23.  
  24.         }
  25.       }
  26.     }
  27.   }
  28.  
  29.   //show success message
  30.   echo "<h1>Uploaded:</h1>";    
  31.   if(is_array($files)){
  32.     echo "<ul>";
  33.     foreach($files as $file){
  34.       echo "<li>$file</li>";
  35.     }
  36.     echo "</ul>";
  37.   }
  38. }
  39. ?>
  40.  
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44.   <title>Upload Files</title>
  45.   <meta charset="utf-8">
  46.   <meta name="viewport" content="width=device-width, initial-scale=1">
  47.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  48.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  49.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  50. </head>
  51. <body>
  52.   <form action="" enctype="multipart/form-data" method="post">
  53.  
  54.     <div class="form-group">
  55.       <label for='upload'>Add Attachments:</label>
  56.       <input class="btn btn-default btn-block" id='upload' name="upload[]" type="file" multiple="multiple" />
  57.     </div>
  58.  
  59.     <p><button type="submit" class="btn btn-primary btn-block" name="submit" value="Submit">Upload</button></p>
  60.  
  61.   </form>
  62.  
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement