Reputation: 9002
I would like to create a multifile file uploader using jQuery.
Below is the code I am working with. I am just trying to abstract out the essentials to uploading multiple files at once using jQuery (and File API) and PHP. If anyone has a simplified answer feel free to share or offer suggestions.
Right now this code works to upload the images(kinda), however. There are issues:
Here is the js/jquery I am working with:
function html5Upload($form) {
file = $form.data('input');
if (file) {
var fd = new FormData();
fd.append($form.find(selector).attr('name'), file);
//get other form input and append to formData
$form.find(':input').each(function () {
if (this.type != 'file') {
fd.append($(this).attr('name'), $(this).val());
}
});
//Upload using jQuery AJAX
jqxhr = $.ajax({
url: $form.attr('action'),
data: fd,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function () {
alert("Images Uploaded!");
}
});
}
$form.remove();
}
$('form').submit(function (event) {
event.preventDefault();
html5Upload($('form.mupload'));
return false;
});
HTML
<form class="mupload" action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" class="fileUpload" multiple>
<button type="submit">Upload</button>
</form>
PHP
/*
Easy PHP Upload - version 2.32
A easy to use class for your (multiple) file uploads
Copyright (c) 2004 - 2010, Olaf Lederer
*/
include("easy_upload/upload_class.php");
$upload = new file_upload();
$upload->upload_dir = 'uploads/';
$upload->extensions = array('.png', '.jpg', '.zip', '.pdf'); // specify the allowed extensions here
$upload->rename_file = true;
if(!empty($_FILES)) {
$upload->the_temp_file = $_FILES['userfile']['tmp_name'];
$upload->the_file = $_FILES['userfile']['name'];
$upload->http_error = $_FILES['userfile']['error'];
$upload->do_filename_check = 'y'; // use this boolean to check for a valid filename
if ($upload->upload()){
echo '<div id="status">success</div>';
echo '<div id="message">'. $upload->file_copy .' Successfully Uploaded</div>';
//return the upload file
echo '<div id="uploadedfile">'. $upload->file_copy .'</div>';
} else {
echo '<div id="status">failed</div>';
echo '<div id="message">'. $upload->show_error_string() .'</div>';
}
}
Upvotes: 0
Views: 1742
Reputation: 14887
I've seen a few potential problem so far:
Only one file uploaded.
name
attributes of the file input should be something like userfiles[]
. This way it will not be overwritten. FYI: if two HTML input have the same name without []
, only the latter will be send over the request. []
can also be used to send any data that have multiple values such as select
(think of Array, and it is represented as array in $_POST).I guess you included the script tag before the form
? Try put the event code inside jQuery domReady like so
.
$(function() {
$('form').submit(function (event) {
event.preventDefault();
html5Upload($('form.mupload'));
return false;
});
});
You might want to check this issue first.
Upvotes: 1