He I don't know if you already found a solution but Maybe you can take a look at this:
first html form
just build a usual html form without submit button but just a button button. note that my solution also has a fancy loading 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 in that fashion to only select the file types you need or want.
then the uploading with jquery/javascript
Looks like yours 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 kept this easy. You can however make a javascript function here to also validate the files and if you want the entire form. just put you validate function name behind beforeSend: youvalfunctname
you can also create a call back there like beforeSend: function(){ //do stuf here }
. And for when there do occur errors in uploading you can do the same with error:
.
The server side php. At last
You can take it form here doe what you want but I just give an example how you could do it.
<?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 take a look at a demo specially made for uploading images: Note not always online
Also here is the code example of the demo