This code does not allow downloading more than 5 files. Here the trick is to reset the input type file, this is what you just cloned and replaced the previous one. I tried with this.reset , but the text of the number of added files remained.
if (window.File && window.FileList && window.FileReader) { $("#files").on("change", function(e) { var files = e.target.files; var filesLength = files.length; var allowedCnt = 5; if (filesLength > allowedCnt) { $(this).replaceWith($(this).val('').clone(true)); alert('Can not add more then ' + allowedCnt + ' files'); return false; } else { for (var i = 0; i < filesLength; i++) { var f = files[i] var fileReader = new FileReader(); fileReader.onload = (function(e) { var file = e.target; $("<img></img>", { class: "imageThumb", src: e.target.result, title: file.name }).insertAfter("#files"); }); fileReader.readAsDataURL(f); } } }); } else { alert("Your browser doesn't support to File API") }
HTML
<form method="POST" action="" id="myform" enctype="multipart/form-data"> <input type="file" id="files" name="files[]" multiple /><br /> <input type="submit" name="submit" value="submit" /> </form>
PHP Here, just connect to the database and add the elements to the table as you want using the foreach loop. Of course, you can also check or check on the server side.
if (isset($_POST["submit"])) { $link = mysqli_connect("myhost", "myuser", "mypassw", "mybd") or die("Error " . mysqli_error($link)); foreach (array_keys($_FILES['files']['tmp_name']) as $key) { //Moving file where you want. mysqli_query($link, "INSERT INTO tblName (fieldName) VALUES ('" . mysqli_real_escape_string($_FILES["files"]['name'][$key]) . "')"); } }
source share