Welcome Guest, Not a member yet? Register   Sign In
Multiple file uploads
#21

[eluser]megabyte[/eluser]
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?
#22

[eluser]mattpointblank[/eluser]
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.
#23

[eluser]fsw[/eluser]
Can anyone tell me ho to make the multi upload get a result like this?
Code:
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
        )

)
#24

[eluser]Nicolas Connault[/eluser]
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:

Code:
$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.
#25

[eluser]Unknown[/eluser]
Master!! thank you so much, it worked perfectly!!
#26

[eluser]Hein[/eluser]
@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.


Code:
<?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;
    }
  
}
#27

[eluser]Wonder Woman[/eluser]
This has just helped me out massively - thanks a lot Big Grin
#28

[eluser]manisha[/eluser]
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?
#29

[eluser]Hein[/eluser]
Hi Manisha,

Thanks for using the code.

Did you set the right 'enctype' for your form? (enctype=multipart/form-data)
#30

[eluser]LuckyFella73[/eluser]
@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!




Theme © iAndrew 2016 - Forum software by © MyBB