Part of the EllisLab Network
   
1 of 4
1
Multiple upload function
Posted: 18 February 2008 05:51 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

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.

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_OPENERR_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:

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

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 17 July 2008 02:32 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  238
Joined  04-11-2008

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,

 Signature 

Joomla! and custom web apps

Profile
 
 
Posted: 17 July 2008 02:40 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

Here is a sample part of a form that I use with this library

<?php
        
if(isset($upload_message))
        
{
            
echo $upload_message;
        
}
    ?>
    <?php $i 
6; for ($j 1$j <= $i$j++): ?>
    
<div class="form_element">
        <
label for="photo<?=$j?>">Photo N°<?=$j?></label>
        
<?=form_upload(array('name'     => 'photo'.$j,
                            
'id'        => 'photo'.$j,
                            
'size'      => '36'))?>
    
</div>
    
<?php endfor ?> 
 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 17 July 2008 03:13 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  238
Joined  04-11-2008

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!

 Signature 

Joomla! and custom web apps

Profile
 
 
Posted: 17 July 2008 03:17 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

Cool that it works !

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 20 February 2009 03:33 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  2
Joined  01-31-2009

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,

Profile
 
 
Posted: 20 February 2009 06:43 AM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1089
Joined  05-17-2008
chero - 20 February 2009 08:33 AM

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

You have to adjust

$config['allowed_types''gif|jpg|jpeg|jpe|png'

Have a look at the user guide: File Uploading Class

Profile
 
 
Posted: 23 February 2009 05:58 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  1
Joined  02-23-2009

Conditional not need:

if( ! empty($value['name'])) 
Profile
 
 
Posted: 23 February 2009 06:03 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006
lusever - 23 February 2009 10:58 AM

Conditional not need:

if( ! empty($value['name'])) 

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

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
Posted: 05 October 2009 08:35 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  34
Joined  04-18-2009

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!!

Profile
 
 
Posted: 05 October 2009 08:43 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  801
Joined  04-20-2006

As the method returns the $files array it is easy to get back the file name from it (just print_r the returned value to see what exactly).

 Signature 

Un blog seo white-hat expliquant quelques techniques intéressantes sur le seo black hat

Ionize CMS - Webdesigner CMS based on CodeIgniter
www.ionizecms.com

My website: Webagency Too Pixel

Profile
 
 
   
1 of 4
1