He does not know if you have already found a solution, but maybe you can take a look at this:
first html form
just create a regular html form without a submit button, but just a button. note that my solution also has a fantastic download bar!
<form enctype="multipart/form-data" id="myform"> <input type="text" name="some_usual_form_data" /> <br> <input type="text" name="some_other_usual_form_data" /> <br> <input type="file" multiple name="file[]" id="image" /> <sub>note that you have to use [] behind the name or php wil only see one file</sub> <br> <input type="button" value="Upload files" class="upload" /> </form> <progress value="0" max="100"></progress> <hr> <div id="content_here_please"></div>
now you can add accept="image/*" or something like that to select only the file types you need.
then loading using jquery / javascript
Sounds like you, but then better.
$(document).ready(function () { $('body').on('click', '.upload', function(){ // Get the form data. This serializes the entire form. pritty easy huh! var form = new FormData($('#myform')[0]); // Make the ajax call $.ajax({ url: 'action.php', type: 'POST', xhr: function() { var myXhr = $.ajaxSettings.xhr(); if(myXhr.upload){ myXhr.upload.addEventListener('progress',progress, false); } return myXhr; }, //add beforesend handler to validate or something //beforeSend: functionname, success: function (res) { $('#content_here_please').html(res); }, //add error handler for when a error occurs if you want! //error: errorfunction, data: form, // this is the important stuf you need to overide the usual post behavior cache: false, contentType: false, processData: false }); }); }); // Yes outside of the .ready space becouse this is a function not an event listner! function progress(e){ if(e.lengthComputable){ //this makes a nice fancy progress bar $('progress').attr({value:e.loaded,max:e.total}); } }
Believe me, I left it easy. However, you can make a javascript function to also check files and, if you want, the whole form. just return the function name for beforeSend: youvalfunctname , you can also create a callback there like beforeSend: function(){ //do stuf here } . And if loading errors occur, you can do the same with error:
Server php. Finally
You can take the form here however you want, but I will just give an example of how you could do this.
<?php $succeed = 0; $error = 0; $thegoodstuf = ''; foreach($_FILES["file"]["error"] as $key => $value) { if ($value == UPLOAD_ERR_OK){ $succeed++; //took this from: "https://stackoverflow.com/questions/7563658/php-check-file-extension" //you can loop through different file types $file_parts = pathinfo($filename); switch($file_parts['extension']) { case "jpg": //do something with jpg break; case "exe": // do sometinhg with exe break; case "": // Handle file extension for files ending in '.' case NULL: // Handle no file extension break; } $name = $_FILES['file']['name'][$key]; // replace file to where you want copy($_FILES['file']['tmp_name'][$key], './upload/'.$name); $size = filesize($_FILES['file']['tmp_name'][$key]); // make some nice html to send back $thegoodstuf .= " <br> <hr> <br> <h2>File $succeed - $name</h2> <br> give some specs: <br> size: $size bytes "; } else{ $error++; } } echo 'Good lord vader '.$succeed.' files where uploaded with success!<br>'; if($error){ echo 'shameful display! '.$error.' files where not properly uploaded!<br>'; } echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_usual_form_data']; echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_other_usual_form_data']; echo $thegoodstuf; ?>
You can also watch a demo specially created for uploading images: Note is not always online
Demo code example is also provided.