CodeIgniter Forums
Multiple upload function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Multiple upload function (/thread-6215.html)

Pages: 1 2 3 4


Multiple upload function - El Forum - 02-18-2008

[eluser]Référencement Google[/eluser]
Hi,

I give a piece of code that can be usefull for some people that will want to take the time understanding the function. It upload files coming from a multiple upload form and if all ok return a files array, other wise delete the uploaded files from server.

It's sure far from perfect and I release it as is, it's part of an application I have developped, so if you are interrested about use this function, you must of course adapt it for your application.

Also note that I will answer basic questions about it, but I won't give any other kind of support for this.

Code:
function multiple_upload($upload_dir = 'uploads', $config = array())
{
    $CI =& get_instance();
    $files = array();

    if(empty($config))
    {
        $config['upload_path']   = realpath($upload_dir);
        $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
        $config['max_size']      = '2048';
    }
        
        $CI->load->library('upload', $config);
        
        $errors = FALSE;
        
        foreach($_FILES as $key => $value)
        {            
            if( ! empty($value['name']))
            {
                if( ! $CI->upload->do_upload($key))
                {                                          
                    $data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                    $CI->load->vars($data);
                        
                    $errors = TRUE;
                }
                else
                {
                    // Build a file array from all uploaded files
                    $files[] = $CI->upload->data();
                }
            }
        }
        
        // There was errors, we have to delete the uploaded files
        if($errors)
        {                    
            foreach($files as $key => $file)
            {
                @unlink($file['full_path']);    
            }                    
        }
        elseif(empty($files) AND empty($data['upload_message']))
        {
            $CI->lang->load('upload');
            $data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE;
            $CI->load->vars($data);
        }
        else
        {
            return $files;
        }
    }

Sample usage:
Code:
if( ! $files = $this->upload->multiple_upload('public/photos'))
{
    echo 'Something went wrong during upload';
}
else
{
    echo 'Upload success !<br />';
    echo '<pre>';
    print_r($files);
    echo '</pre>';
}



Multiple upload function - El Forum - 07-17-2008

[eluser]CARP[/eluser]
Hi Too Pixel
I've tried your library, and worked great!
But, how would you modify its code for example to

Allow user to submit from 1 to 5 images. I mean, I've made a userfile[] set of 5 upload fields in my form, the first one is mandatory, but the other 4 not. But when I use your lib, I get errors, because it requires the user to select 5 images, 1 for each upload field...

Thanks a lot in advance,


Multiple upload function - El Forum - 07-17-2008

[eluser]Référencement Google[/eluser]
Here is a sample part of a form that I use with this library
Code:
&lt;?php
        if(isset($upload_message))
        {
            echo $upload_message;
        }
    ?&gt;
    &lt;?php $i = 6; for ($j = 1; $j <= $i; $j++): ?&gt;
    <div class="form_element">
        <label for="photo&lt;?=$j?&gt;">Photo N°&lt;?=$j?&gt;</label>
        &lt;?=form_upload(array('name'     => 'photo'.$j,
                            'id'        => 'photo'.$j,
                            'size'      => '36'))?&gt;
    </div>
    &lt;?php endfor ?&gt;



Multiple upload function - El Forum - 07-17-2008

[eluser]CARP[/eluser]
Puff!
Just naming the upload fields from userfile[] to userfile1, userfile2... etc. solved the problem!!

1. I thought the upload files should be named userfile
2. and that they had to been toghether in an array...

Thanks!


Multiple upload function - El Forum - 07-17-2008

[eluser]Référencement Google[/eluser]
Cool that it works !


Multiple upload function - El Forum - 02-20-2009

[eluser]Unknown[/eluser]
Thanks for this script!

Great it's work, I can uploading more than one images in one time

but
I got a problem when I try uploading some zip and pdf file,


Multiple upload function - El Forum - 02-20-2009

[eluser]pistolPete[/eluser]
[quote author="chero" date="1235136815"]I got a problem when I try uploading some zip and pdf file[/quote]
You have to adjust
Code:
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
Have a look at the user guide: File Uploading Class


Multiple upload function - El Forum - 02-23-2009

[eluser]Unknown[/eluser]
Conditional not need:
Code:
if( ! empty($value['name']))



Multiple upload function - El Forum - 02-23-2009

[eluser]Référencement Google[/eluser]
[quote author="lusever" date="1235404723"]Conditional not need:
Code:
if( ! empty($value['name']))
[/quote]

Yes that's right, thanks for seeing this one.


Multiple upload function - El Forum - 10-05-2009

[eluser]artlover[/eluser]
thanks for great code. but I also need to save it to database. so I need the name of files :/ how can I get the names?

ps, if there is already same named file at folder, it automatically rename the file... in this case, I need renamed file name :/

appreciate helps!!