Multiple Uploads with JQuery and Code Igniter

This one goes out to all the code writers, web developers, and open source script fans.

This is a short script I built for a project, ended up opting to use individual AJAX uploads rather than multiple uploads at once. Yet a script this useful deserves to see the rest of the world so I give you.

Multiple File Uploads with JQuery and Code Igniter

To get it running extract into the root directory of a Code Igniter installation, included are:

The upload library is based upon the CI upload library, it will check for attacks, and check whether the files uploaded are images or something else. You can restrict the files to any extension(s), if you need more security than that then I’ll assume you know enough to add in the function you need.

The beauty of this script is the simplicity, with a few lines of code you can allow for multiple uploads from a single page, then let this script handle the uploads and pass on information about everything that happened.

If you are not familiar with Code Igniter it is a rather lightweight PHP MVC framework. Which is a geeky way of saying that it is a programming framework that can save you time without eating your server. Of course the same goes for JQuery, which is for Javascript framework that will save you time.

If you have any questions please leave a comment or ask me through the contact form.

Tags: , ,

39 Responses to “Multiple Uploads with JQuery and Code Igniter”

  1. Hyperfire says:

    Awesome, your script is flawless! Plug“n play kind.
    Awesome job to make it available for free and integrated with CI. Many thanks.

  2. yomi says:

    this is great library, it’s just what I needed but I have a problem;
    it only works with this included jquery.MultiFile ! now, there is a newer version of this Multi file and when I use it it wont work. For example, I need to change “remove” string when removing selected image in some other language, and when i change it in your packed js file – the script won’t execute. you think you could solve this problem?

  3. yomi says:

    sorry, i solved this problem with MetaData plugin and now works great! Thanks again!

  4. I am having problem using this library, I cannot see error messages when the script encounters errors. I have echoed the errors in view files and passed the error variable through the data array in view.

  5. Phil says:

    I have to change line 33 of the Multi_upload.php controller from

    $num_files = count($_FILES[$field]['name']) -1;

    to

    $num_files = count($_FILES[$field]['name']);

    for it to work with jQuery 1.3.1 (up from the 1.2.6 included) and jQuery Multiple File Upload Plugin
    v1.46 (up from the v1.3 included).

  6. Alvin says:

    thank you for the update, will get around to updating the released version at some point

  7. Vinod says:

    I want to use multiple upload. Can you please help. Bcos example only shows 1 file upload.

  8. Marcin says:

    For me, for jQuery 1.3.1 and CodeIgniter 1.7.1, this fantastic script doesn’t work correctly. MiniSOLVED:
    In Multi_upload.php, Line 156:
    if($error_hold[$i]==false) { // MOJA PRZEROBKA (jesli nie ma bledu pozwalamy na copy)
    if ( ! @copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
    {
    if ( ! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
    {
    $error_hold[$i] = ‘upload_destination_error’;
    }
    }
    }
    instead of
    if ( ! @copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
    {
    if ( ! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
    {
    $error_hold[$i] = ‘upload_destination_error’;
    }
    }
    and, line 212: $CI->upload->set_error($error_hold); instead of $this->set_error($error_hold);

  9. Ortzinator says:

    Thanks!

    One bug with CI 1.7.2

    In CI’s upload lib, Upload.php, comment out line 935

    $filename = $this->file_name;

    Not sure what this does anyway. Seems unnecessary.

  10. Mike Kelly says:

    Just wanted to say thanks for the script — worked perfectly and saved me tons of time! Thanks man

  11. Faluvegi Ferenc says:

    Thank you man! In my case the script works “out of box” :) .

    Regards,
    Ferenc.

  12. Aline says:

    Thank! Perfect! =)

  13. Aline Matos says:

    Hello there, great job! =)
    I just have to change the line 92:

    $CI->upload->file_name = $_FILES[$field]['name'][$i];

    instead of

    $CI->upload->file_name = $CI->upload->_prep_filename($_FILES[$field]['name'][$i]);

    The name of the files dont repeat to me this way.

    Thanks again! =)

  14. Matt Shelley says:

    Thanks you for this script!!! Very useful and works great… Took some testing and reading through the comments to make the mods suggested, but its up and going now. Great Work!!

  15. Yash says:

    wow this is awesome
    just paste files and it’s ready
    great coding and thank you for sharing with

  16. icex says:

    hi,

    i have a little problem, i got the script and on my pc is working fine, but when i try to upload it to my server it give’s me an error: “You did not select a file to upload”.

    Many thanks

  17. Wicak says:

    I keep getting bool(false) aArray as an answer? please tell me what happen?

  18. Wicak says:

    I fix it, now some other challenge, how to make it return a array like this:

    Array
    (
    [3] => Array
    (
    [op_id] => 17
    [currency_machine] => 1
    [machine_amount] => 123
    [currency_disbursment] => 1
    [disbursment_amount] => 123
    [currency_sum_insured] => 1
    [sum_insured_total] => 123
    )

    [1] => Array
    (
    [op_type_id] => 23
    [op_id] => 23
    [currency_machine] => 1
    [machine_amount] => 123
    [currency_disbursment] => 1
    [disbursment_amount] => 123
    [currency_sum_insured] => 1
    [sum_insured_total] => 123
    )

    [2] => Array
    (
    [op_id] => 22
    [currency_machine] => 1
    [machine_amount] => 123
    [currency_disbursment] => 1
    [disbursment_amount] => 123
    [currency_sum_insured] => 1
    [sum_insured_total] => 123
    )

    )

  19. Simon says:

    Hi,

    I am using your library, and I have small issue, if I upload for example 3 JPGs, the images go into my upload folder as,

    image1,jpg
    image2,jpg,jpg
    image3,jpg,jpg,jpg

    I was wondering if there is away to get around this? Everything else works perfectly

  20. Sven says:

    How to upload any file and maximum size?

  21. Tim Farland says:

    You are the man. This worked spot-on, first time!

  22. Moch Yasin says:

    I just wanna say thanks so much for useful codes.

  23. ghprod says:

    wow, its works perfectly!

    thanks, good job!!!

    reagrds

  24. sofwan says:

    Thank You very much, it helps me a lot and it works well.

  25. marcus says:

    cool stuff!

  26. sirhan says:

    Hi,

    Thanks for the script, really helpful. Just wondering how to change the [remove] link to an icon.

    Tried following some of the instructions from the link below but still doesn’t change anything. So anybody know how to fix this??

    http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Examples

  27. sathin says:

    I’ve made the modifications shown in the comments here, but still no joy in getting the error messages to show up. The are there, if I do a var_dump in the libraries/Upload.php file I can see them; they just don’t get passed through to the view.

    Any new insights?

    DS

  28. Vishal says:

    Hey,

    Thanks for the code as it works really good but there is one problem.

    When i select more then one file and i hit upload… the pictures are saved but it comes back to the same upload_form with no error message.

    Only when i select one file to upload i am redirected to the upload_success page….

    Any ideas?

    Thanks

  29. Luis says:

    Hi, awesome work dude. I have a question, as I see the code(JS) you give us is packed or encrypted, anyway.. I wanted to change the word “remove” and the script crashes.. How can I decompile to see the entire code and see what’s wrong?

    Thanks again for your time, keep up the good work!

  30. P3rikl3s says:

    how I can display an image before uploading?

  31. saric says:

    Hi there,
    I’m using this CI library and jquery plugin and it is great. Thank you for that.

    Now I’m solving one problem and I don’t know how to do it. When you submit the form and there is an error, the form will display again but the upload component does not display the files which were submited. The files are in the post object. Anyone knows how to make the Multi_upload plugin to show these files?

    Thanks in advance for the answer.

  32. You can get this to work easily with CI 2.0 and the latest jquery.MultiFile, but you need to modify the Multi_upload.php library file.

    Around line 31, change
    $num_files = count($_FILES[$field]['name'])-1;
    by removing the -1 at the end. I have no idea why it’s there in the first place.

    Then, if you’re running PHP5, it will complain about using the protected method of $CI->upload->_prep_filename().

    Just copy that method from the core Upload.php library to Multi_upload.php, changing the calls to $this-> to $CI->upload->, and adding $CI =& get_instance() at the beginning of the method.

    Finally, change the call to $CI->upload->_prep_filename(…) to $this->_prep_filename(…).

    Voila! Works like a dream!

  33. Karthick says:

    Upload error not working for me. I reduced the Kb size to 10 and uploaded 546 kb image file but it was uploaded.

    Please help me to sort out this problem.

    Thanks

    Karthick

  34. Alvin says:

    @Bob please describe what you mean

  35. Alvin says:

    I believe the newer version of this script does, I’ve not use it for a couple years.

    You would need to adjust the script to work with the new version, I believe the solution is among these comments.

  36. Alvin says:

    Thank you Nicolas

  37. Menuka says:

    Hi All,

    as i see following restrictions doesn’t work ???

    $config['max_size'] = ‘50′; // in kb
    $config['max_width'] = ‘300′;
    $config['max_height'] = ‘300′;

    you can upload a file which is greater than 50kb… etc

  38. Youcef says:

    Hey Guyn

    Awesome plugin. Just I want to
    - rename the file want to upload it before the upload,
    - add a text field for each image uploaded

    Thanks.

Leave a Reply