HTML5 Image upload with javascript and php

HTML5 Image upload with javascript and php

I want to multi-upload images to a database using javascript and php (html5), but I have problem with it, my code is just uploading one image, which is the latest photo selected and not all. How can fix this problem and what do I have to do?

I use developer mozilla html5: Here

You can test my code by selecting multi images and clicking on button Upload Image to see the result.

DEMO FROM MY FULL CODDE

My js code:
  1. function doClick() {
  2.     var el = document.getElementById("fileElem");
  3.     if (el) {
  4.         el.click();
  5.     }
  6. }

  7. function handleFiles(files) {
  8.     var d = document.getElementById("fileList");
  9.     if (!files.length) {
  10.         d.innerHTML = "<p>No files selected!</p>";
  11.     } else {
  12.         var list = document.createElement("ul");
  13.         d.appendChild(list);
  14.         for (var i = 0; i < files.length; i++) {
  15.             var li = document.createElement("li");
  16.             list.appendChild(li);

  17.             var img = document.createElement("img");
  18.             img.src = window.URL.createObjectURL(files[i]);;
  19.             img.height = 60;
  20.             img.onload = function () {
  21.                 window.URL.revokeObjectURL(this.src);
  22.             }
  23.             li.appendChild(img);

  24.             var info = document.createElement("span");
  25.             info.innerHTML = files[i].name + ": " + files[i].size + " bytes";
  26.             li.appendChild(info);
  27.         }
  28.     }
  29. }

My php code:

  1. <?php 
  2.   foreach ($_FILES[ 'userfile'] as $key=>$value{ 
  3.       echo '<pre>';
  4.       print_r($value);
  5.   }
  6. ?>
My html code:
  1. <html>

  2.     <head>
  3.         <title>file input click() demo</title>
  4.         <script type="text/javascript" src="html5java.js"></script>
  5.     </head>

  6.     <body>
  7. <a href="javascript:doClick()">Select some files</a>

  8.         <div id="fileList"></div>
  9.         <form action="up.php" method="post" enctype="multipart/form-data">
  10.             <input type="file" name="userfile[]" id="fileElem" multiple accept="image/*"
  11.             style="display:none" onchange="handleFiles(this.files)">
  12.             <input type="submit" value="Upload Images">
  13.         </form>
  14.     </body>

  15. </html>