Part of the EllisLab Network
   
2 of 3
2
Multiple file uploads
Posted: 14 February 2009 03:48 PM   [ Ignore ]   [ # 16 ]  
Grad Student
Avatar
Rank
Total Posts:  56
Joined  12-19-2007
CrustyDOD - 14 February 2009 07:57 PM
Bob Sawyer - 13 February 2009 10:41 PM

Disregard… I had made some modifications to the jquery.MultiFile.js file that were causing problems.

Can you post/tell the modifications you did?
I’m having the same problem and can’t figure it out :S

I was having problems getting the whole ‘STRING: { remove: }’ stuff to work, so I hardcoded the icon image I wanted to use to replace the ‘[remove’] that is usually placed next to the file.

 Signature 

Bob Sawyer
Head Chef, Web Design & Development
Pixels and Code, LLC
www.pixelsandcode.net

Profile
 
 
Posted: 14 February 2009 05:19 PM   [ Ignore ]   [ # 17 ]  
Summer Student
Total Posts:  16
Joined  10-19-2008

Ok, it doesn’t work cause the function returns FALSE since there’s an error 4 aka no file uploaded. And this comes from the fact that JQuery Multiple Upload always submits 1 empty field.

Here it return FALSE since case 4 exists..

if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))

            
{

                $error 
= ( ! isset($_FILES[$field]['error'][$i])) ? $_FILES[$field]['error'][$i];



                switch(
$error)

                
{

                    
case 1:  // UPLOAD_ERR_INI_SIZE

                        
$error_hold[$i] 'upload_file_exceeds_limit';

                    break;

                    case 
2// UPLOAD_ERR_FORM_SIZE

                        
$error_hold[$i] 'upload_file_exceeds_form_limit';

                    break;

                    case 
3// UPLOAD_ERR_PARTIAL

                       
$error_hold[$i] 'upload_file_partial';

                    break;

                    case 
4// UPLOAD_ERR_NO_FILE

                       
$error_hold[$i] 'upload_no_file_selected';

                    break;

                    case 
6// UPLOAD_ERR_NO_TMP_DIR

                        
$error_hold[$i] 'upload_no_temp_directory';

                    break;

                    case 
7// UPLOAD_ERR_CANT_WRITE

                        
$error_hold[$i] 'upload_unable_to_write_file';

                    break;

                    case 
8// UPLOAD_ERR_EXTENSION

                        
$error_hold[$i] 'upload_stopped_by_extension';

                    break;

                    default :

                        
$error_hold[$i] 'upload_no_file_selected';

                    break;

                
}



                
return FALSE;

            

??

Profile
 
 
Posted: 15 February 2009 11:14 PM   [ Ignore ]   [ # 18 ]  
Grad Student
Rank
Total Posts:  46
Joined  09-21-2008

try copying the original JQuery file over and check that,

if that’s a nogo try

print_r($_FILES); 
Profile
 
 
Posted: 16 February 2009 07:36 AM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  16
Joined  10-19-2008

I’ve fixed it by looping over $_FILES and removing empty fields before that lines.

I’m using the latest jquery..

Just curious how come that this is working for some people when its obvious that the error will stop the function since empty field is always submitted with form.. Or does it only work with some old version of jquery.. Weird..

Profile
 
 
Posted: 26 March 2009 08:21 PM   [ Ignore ]   [ # 20 ]  
Lab Assistant
RankRank
Total Posts:  276
Joined  03-21-2007

I’m having an issue with Multiple Uploads with JQuery and Code Igniter found on mitesdesign.com

If I upload an image it returns data.

If I upload a word doc it uploads it but returns an empty error array.

I use open office but save my files as .doc which I checked.

If I try to upload a pdf or text file it works just fine as long as I only upload a single file.

***update to it only returning data if there is a single file. I switched to the jquery-1.2.6 and multi file upload script that mitedesigns provides. I had updated and was also usuingthe jquery medadata file which for some reason causes issues.

Anyone have this problem or have a solution?

Profile
 
 
Posted: 12 July 2009 10:54 AM   [ Ignore ]   [ # 21 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  06-11-2009

I had some issues with this library - when I was uploading 3 files, only 2 were added. If I uploaded 1, nothing happened at all (no error). I changed line 33 ($num_files = count($_FILES[$field][‘name’])-1; to $num_files = count($_FILES[$field][‘name’]); (eg removed the -1 part) and it worked fine.

I already had the jQuery script downloaded before checking out this script, so it’s possible that the version I’m using is different to the one included in the download, and doesn’t have the extra field thing. Either way, if anyone else hits that bug, that’s why.

Profile
 
 
Posted: 15 December 2009 03:02 AM   [ Ignore ]   [ # 22 ]  
Summer Student
Total Posts:  8
Joined  12-23-2008

Can anyone tell me ho to make the multi upload get a result 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
        
)

Profile
 
 
Posted: 08 February 2011 04:26 AM   [ Ignore ]   [ # 23 ]  
Summer Student
Avatar
Total Posts:  17
Joined  07-22-2010

I upgraded Alvin Mites’ Multi_upload library so that it would work with CI 2.0 (running on PHP5) and the latest version of jquery.MultiFile.

Just start with the regular CI Upload class, then just after you’ve called $ci->load->library(‘upload’, $config), take over with the multi_upload class:

$CI->load->library('Multi_upload');
$files $CI->multi_upload->go_upload('field_name_of_your_choice_without_square_brackets'); 

That’s it. You don’t even need the jquery script, this will work with an array of files set up manually in HTML as well.

File Attachments
Multi_upload.zip  (File Size: 3KB - Downloads: 103)
Profile
 
 
Posted: 18 February 2011 04:39 PM   [ Ignore ]   [ # 24 ]  
Summer Student
Total Posts:  1
Joined  02-18-2011

Master!! thank you so much, it worked perfectly!!

Profile
 
 
Posted: 24 February 2011 12:46 PM   [ Ignore ]   [ # 25 ]  
Summer Student
Total Posts:  11
Joined  07-08-2009

@Colin Williams: thanks for the code! I had some time and build on your code to make it give error messages, data array and FAIL or SUCCES for every file uploaded.

When uploading files with their names as array ‘fieldname[]’ the following return values change:
- do_upload() returns an array with TRUE or FALSE values telling you which file failed when uploading
- display_errors() returns an array of error messages returned per file upload.
- data() returns an array of data arrays as returned per file upload

Hope this helps.

Edit 26/02/2011:
- display_errors() now gives back an array of the error messages
- the original upload class generates an error message for file fields that were left blank. This class ignores those errors and sets the ‘success’ to ‘NULL’ so you can filter those out.

<?php

class MY_Upload extends CI_Upload {
   
    
var $my_config;
    var 
$my_error_msg = array();
    var 
$my_data = array();
    var 
$error_count 0;
    
    function 
MY_Upload($props = array())
    
{
        $this
->my_config $props;
        
parent::CI_Upload($props);
    
}
    
    
function do_upload($field 'userfile')
    
{
        $success 
FALSE;
        if (isset(
$_FILES[$field]) and is_array($_FILES[$field]['error']))
        
{
            
// Create a pseudo file field for each file in our array
            
for ($i 0$i count($_FILES[$field]['error']); $i++)
            
{
                
// Give it a name not likely to already exist!
                
$pseudo_field_name '_psuedo_'$field .'_'$i;
                
// Mimick the file
                
$_FILES[$pseudo_field_name] = array(
                    
'name'     => $_FILES[$field]['name'][$i],
                    
'size'     => $_FILES[$field]['size'][$i],
                    
'type'     => $_FILES[$field]['type'][$i],
                    
'tmp_name' => $_FILES[$field]['tmp_name'][$i],
                    
'error' => $_FILES[$field]['error'][$i]
                    
);

                if (
$_FILES[$field]['error'][$i] != 4)
                
{
                    
// Let do_upload work it's magic on our pseudo file field
                    
$success[$i]         parent::do_upload($pseudo_field_name);
                    
$this->my_data[]    parent::data();
                    
$this->my_error_msg    array_merge($this->my_error_msg$this->error_msg);
                
else {
                    
// Let do_upload work it's magic on our pseudo file field
                    
parent::do_upload($pseudo_field_name);
                    
// Since this is an empty field, set $succes to NULL
                    
$success[$i]         'NULL';
                    
$this->my_data[]    parent::data();
                    
$this->my_error_msg    array_merge($this->my_error_msg$this->error_msg);
                
}
                
                
// Count the errors
                
if ($success[$i] === FALSE)
                
{
                    $this
->error_count++;
                
}
                
                
// Re-initialize after every file
                
parent::initialize($this->my_config);
            
}
        }
        
else if (isset($_FILES[$field]))
        
// Works just like do_upload since it's not an array of files
        
{
            $success 
parent::do_upload($field);
        
}
        
return $success;
    
}
    
    
/**
     * Display the error message
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string | array
     */    
    
function display_errors($open '<p>'$close '</p>')
    
{
        
if (count($this->my_error_msg))
        
{
            $arr 
= array();
            
            foreach (
$this->my_error_msg as $val)
            
{
                $arr[] 
$open.$val.$close;
            
}
            
return $arr;
        
else {
            $str 
'';
            
            foreach (
$this->error_msg as $val)
            
{
                $str 
.= $open.$val.$close;
            
}
            
return $str;
        
}
    }
    
    
/**
     * @acces    public
     * @return    integer
     */
    
function error_count()
    
{
        
return $this->error_count;
    
}
    
    
/**
     * Finalized Data Array
     *    
     * Returns an associative array containing all of the information
     * related to the upload, allowing the developer easy access in one array.
     *
     * @access    public
     * @return    array
     */    
    
function data()
    
{
        
return $this->my_data;
    
}
   
Profile
 
 
Posted: 23 March 2011 06:46 AM   [ Ignore ]   [ # 26 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  120
Joined  07-19-2010

This has just helped me out massively - thanks a lot :D

Profile
 
 
Posted: 29 February 2012 04:48 AM   [ Ignore ]   [ # 27 ]  
Summer Student
Total Posts:  11
Joined  09-16-2010

I tried to use the code Multi_upload class file in my code to upload multiple files ... but it is giving me error “You did not select a file to upload.” when I click on ‘Upload’ button…

when I run the files separately the code executed successfully and the uploaded files get stored in the system under specific folder… but when i try to embed the file upload code in other file for multiple file upload … it gives me error “You did not select a file to upload.” though I select one file or multiple files….

It is actually getting $_FILES as an empty array when submit the form ….

Can you please help me to resolve this issue?

Profile
 
 
Posted: 29 February 2012 05:07 AM   [ Ignore ]   [ # 28 ]  
Summer Student
Total Posts:  11
Joined  07-08-2009

Hi Manisha,

Thanks for using the code.

Did you set the right ‘enctype’ for your form? (enctype=multipart/form-data)

Profile
 
 
Posted: 29 February 2012 05:15 AM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  794
Joined  03-25-2006

@Nicolas Connault

I tryed to download your .zip file but just get a blank browser page.
Would you check the file is still available? Would be nice. Thank you!

Profile
 
 
Posted: 29 February 2012 05:22 AM   [ Ignore ]   [ # 30 ]  
Summer Student
Total Posts:  11
Joined  09-16-2010

Hi Hein,

Thanks for immediate reply…
Yes I have used (enctype=multipart/form-data) for my form…

Profile
 
 
   
2 of 3
2