0

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:

  1. Even after selecting more than one file, only one file uploads to the directory
  2. The form changes the page upon submit

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>';

    }
}
2
  • What does the title have to do with your question? Commented Aug 27, 2012 at 5:57
  • oops. My bad I forgot to change it. StackOverflow saved my last question in the title Commented Aug 27, 2012 at 6:00

1 Answer 1

1

I've seen a few potential problem so far:

  1. Only one file uploaded.

    • For multiple file upload, I think the 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 don't really know your upload handler, are you sure it can handle multiple file upload?
  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.