EllisLab text mark
Advanced Search
1 of 3
1
   
Latavish’s Multiple Image Upload with Thumbnail Generation
Posted: 27 May 2008 10:41 AM
Avatar
Joined: 2008-03-27
26 posts

Latavish’s Multiple Image Upload w/ Thumbnail Generation

Hi CI lovers,
I hope you guys had a GREAT and EXTENDED weekend. Been working on this for a few days now and finally finished up this weekend and wanted to share my work with this great community. Now this code is really customizable so feel free to trick it out to fit your needs. Code may not be perfect because I designed this to fit my needs on a project that i’m doing. (a nice CMS) If you find this code useful all I require is a simple thanks! TeeHee!! grin Happy Coding…

Features:
Upload Multiple Images at once
Automatically Renames Images to a random name (protects file from being overwritten)
Adds Image Information to DB

C O N T R O L L E R

class Upload extends Controller {

    
function Upload()
    
{
        parent
::Controller();
        
$this->load->helper(array('form','url','file'));
        
    
    
}
    
    
function index()
    
{
        
        $this
->load->view('upload/upload_index'); //Upload Form
        
    
}

    
function picupload()
    
{
        
//Load Model
        
$this->load->model('Process_image');

        
$config['upload_path''./uploads/';
        
$config['allowed_types''gif|jpg|png';
        
$config['max_size']    '2048'//2 meg


        
$this->load->library('upload');

        foreach(
$_FILES as $key => $value)
        
{
            
if( ! empty($key['name']))
            
{
                $this
->upload->initialize($config);
        
                if ( ! 
$this->upload->do_upload($key))
                
{
                    $errors[] 
$this->upload->display_errors();
                    
                
}    
                
else
                
{

                    $this
->Process_image->process_pic();

                
}
             }
        
        }
        
        
        $data[
'success''Thank You, Files Upladed!';

        
$this->load->view('upload/upload_pictures'$data); //Picture Upload View

        
        
        
    

M O D E L

class Process_image extends Model {
    
    
function Process_image()
    
{
        parent
::Model();
        
        
$this->load->library('image_lib');
        
//Generate random Activation code
        
        
function generate_code($length 10){
    
                
if ($length <= 0)
                
{
                    
return false;
                
}
            
                $code 
"";
                
$chars "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
                
srand((double)microtime() * 1000000);
                for (
$i 0$i $length$i++)
                
{
                    $code 
$code substr($charsrand() % strlen($chars), 1);
                
}
                
return $code;
            
                
}
 
    }

function process_pic()
    
{   
        
//Connect to database
        
$this->load->database();
        
        
//Get File Data Info
        
$uploads = array($this->upload->data());
        
        
$this->load->library('image_lib');

        
//Move Files To User Folder
        
foreach($uploads as $key[] => $value)
        
{
            
                        
//Gen Random code for new file name
            
$randomcode generate_code(12);
            
            
$newimagename $randomcode.$value['file_ext'];
            
            
//Creat Thumbnail
            
$config['image_library''GD2';
            
$config['source_image'$value['full_path'];
            
$config['create_thumb'TRUE;
            
$config['thumb_marker''_tn';
            
$config['master_dim''width';
            
$config['quality'75;
            
$config['maintain_ratio'TRUE;
            
$config['width'175;
            
$config['height'175;
            
$config['new_image''/pictures/'.$newimagename;

            
//$this->image_lib->clear();
            
$this->image_lib->initialize($config);
            
//$this->load->library('image_lib', $config);
            
$this->image_lib->resize();
            
            
//Move Uploaded Files with NEW Random name
            
rename($value['full_path'],'/pictures/'.$newimagename);
            
            
//Make Some Variables for Database
            
$imagename $newimagename;
            
$thumbnail $randomcode.'_tn'.$value['file_ext'];
            
$filesize $value['file_size'];
            
$width $value['image_width'];
            
$height $value['image_height'];
            
$timestamp time();
            
            
//Add Pic Info To Database
            
$this->db->set('imagename'$imagename);
            
$this->db->set('thumbnail'$thumbnail);
            
$this->db->set('filesize'$filesize);
            
$this->db->set('width'$width);
            
$this->db->set('height'$height);
            
$this->db->set('timestamp'$timestamp);
            
            
//Insert Info Into Database
            
$this->db->insert('pictures');

        
}
        
        
        
    } 
 Signature 

———————————
Programming Is an Art

 
Posted: 10 June 2008 02:28 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-05-05
15 posts

Thanks for this code…
I try it but it didn’working or maybe is there something wrong with my view file?

————————————————————————————————-

<form action=“http://192.168.1.133/neo/index.php/neomin/picupload” method=“post” enctype=“multipart/form-data”>
  <input type=“file” name=“key” size=“20” />
  <input type=“file” name=“key2” size=“20” />
  <input type=“file” name=“key3” size=“20” />
  <input type=“submit” value=“upload” />
</form>


————————————————————————————————-

 
Posted: 12 July 2008 02:58 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-05-05
15 posts

Thanks lavatish, finally i can make it works…
here is my code

Controller

function upload_tour_pic() {
        $this
->load->helper(array('form','url','file'));
        
$data['title'"Upload Pictures";
        
$data['query'$this->tour_model->get_tour();
        
$this->load->view('upload_tour_pic'$data);
}
     
function picupload()
{
        
//Load Model
        
$this->load->helper(array('form','url','file'));
        
$this->load->model('Process_image');
        
$this->load->model('tourpic_model');
        
$this->load->library('upload');
        
$this->load->library('validation');
        
        
$gmt=false;
        
$time = ($gmt)  ? gmstrftime("%Y-%m-%d %H:%M:%S"time()) : strftime("%Y-%m-%d %H:%M:%S"time());
        
    
        
        
$picInfo = array(
                
'tour_id' => (int) $this->input->post('tour_id'),
                
'timestamp' => $time
        
);

        
        
$config['upload_path''./uploads/';
        
$config['allowed_types''gif|jpg|png';
        
$config['max_size']    '8048'//2 meg

        
        
$data['extraHead'"<link href=\"" .base_url()."css/admin.css\" rel=\"stylesheet\" type=\"text/css\" />";

        
$i=1;
        
$j=1;
        foreach(
$_FILES as $key => $value)
        
{
            
if( ! empty($key['name']))
            
{
                $this
->upload->initialize($config);
                
$data['title''Files Upload!';
                if ( !
$this->upload->do_upload($key))
                
{
                    $data[
'error'][$j] =  $this->upload->display_errors();    
                    
$j++;
                
}    
                
else 
                
{
                    $this
->Process_image->process_pic($picInfo);
                    
$data['success''Thank You, Files Upladed!';
                    
$data['upload_data'][$i]  $this->upload->data();
                    
$i++;
                
}
            }
        }
        
        
        
            $data[
'query'$this->tour_model->get_tour();
            
$data['rowpic'$this->tourpic_model->get_pic_code($picInfo);
            
$data['countdata'count($data['upload_data']);
            
$data['counterr'count($data['error']);
            
            
//$data['query'] = $this->tourpic_model->get_pic_code($picInfo);
            
$this->load->view('upload'$data);
            
            
            
    

now I think i want to add some fields for the image, like title and description…
Do anyone have idea how to loop array of fields and insert it into database..
For example:
image1 - title1 - desc1
image2 - title2 - desc2
image3 - title3 - desc3

<input type="text" name="pic_title[]" value="" />
<
textarea name="pic_desc[]"></textarea>

<
input type="text" name="pic_title[]" value="" />
<
textarea name="pic_desc[]"></textarea>

<
input type="text" name="pic_title[]" value="" />
<
textarea name="pic_desc[]"></textarea

Thanks…

 
Posted: 12 July 2008 07:25 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-06-04
221 posts

Nice code smile


Thanks

 Signature 

http://www.asinox.net
CodeIgniter code and more.

 
Posted: 14 July 2008 10:45 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2008-05-27
46 posts

Hi, very nice tutorial but i have problemm

foreach($_FILES as $key => $value)
{
    
if( ! empty($key['name']))
    
{
    $this
->upload->initialize($config);
    
$data['title''Files Upload!';
    if ( !
$this->upload->do_upload($key))
        

if ( !$this->upload->do_upload($key)) show me an error of

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: libraries/Upload.php

Line Number: 152

View is:

<tr><td>Fotka: <input type=“file” name=“pluspayment_title_photo[]” /></tr>

what i am doing wrong?

 
Posted: 08 August 2008 07:18 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-26
15 posts

I’m using your example in my application, but every once in a while the files get confused.
The user choose 3 files for 3 different purposes and each are moved to their own respective folder. Yet, for some reason files become mismatched intermittently.


Has anyone experienced this?

 
Posted: 21 August 2008 08:07 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-12-14
3 posts
Mitja - 14 July 2008 02:45 PM

Hi, very nice tutorial but i have problem


Hi!
Instead of checking this:

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

try this:

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

In this case don’t use array in file upload fields, add a counter after them:

<input type="file" name="userfile_1" />
<
input type="file" name="userfile_2" />
<
input type="file" name="userfile_3" /> 
 
Posted: 21 August 2008 05:26 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-16
411 posts
donpepito - 21 August 2008 12:07 PM

try this:

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

Why do people even waste CPU time on the empty function?

Just switch to boolean:

if($value['name']
 Signature 

My Blog, C2D, PHP Videos, Résumé, Super .htaccess, Extra hooks, and MicroMVC

 
Posted: 21 August 2008 05:51 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-06
743 posts
Xeoncross - 21 August 2008 09:26 PM
donpepito - 21 August 2008 12:07 PM

try this:

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

Why do people even waste CPU time on the empty function?

Just switch to boolean:

if($value['name']

It’s not the same thing. From the php manual:

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

So this produces an undefined index error:

if (! $_POST['does_not_exist']// undefined index error 

While empty() checks isset() first, so no error is produced:

if (empty($_POST['does_not_exist'])) // no error 
 Signature 

“I am the terror that flaps in the night”

 
Posted: 21 August 2008 06:01 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-16
411 posts
Rick Jolly - 21 August 2008 09:51 PM

So this produces an undefined index error:

if (! $_POST['does_not_exist']// undefined index error 

While empty() checks isset() first, so no error is produced:

if (empty($_POST['does_not_exist'])) // no error 

I was assuming you were using a set variable as it is bad practice to just “assume” one is set/unset. But that is right.

 Signature 

My Blog, C2D, PHP Videos, Résumé, Super .htaccess, Extra hooks, and MicroMVC

 
Posted: 07 February 2009 12:37 AM   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-02-07
52 posts

I’ve just used this with ever slightly modified default code from the User Guide to enable multi-file uploads. It produces multiple views of the results too.

Note: Check your uploads folder path if different than mine. Also, if rewriting your URLs, add the upload folder name to your .htaccess file or you may get errors.

Here is my upload.php file:

<?php

class Upload extends Controller {
    
    
function Upload()
    
{
        parent
::Controller();
        
$this->load->helper(array('form''url','html'));
    
}
    
    
function index()
    
{    
        $this
->load->view('upload_form', array('error' => ' ' ));
    
}

    
function do_upload()
    
{
        $config[
'upload_path''./uploads/';
        
$config['allowed_types''gif|jpg|png';
        
$config['max_size']    '1000';
        
        
$this->load->library('upload');
    
//
     
foreach($_FILES as $key => $value)
        
{
            
if( ! empty($key['name']))
            
{
                $this
->upload->initialize($config);
        
                if ( ! 
$this->upload->do_upload($key))
                
{
                    $errors[] 
$this->upload->display_errors();
                    
                    
$this->load->view('upload_form'$error);
                
}    
                
else
                
{
                    $data[$key] 
= array('upload_data' => $this->upload->data());
            
                    
$this->load->view('upload_success'$data[$key]);
                    

                
}
             }
        
        }
    
//
    
        
    
}    
}
?> 

Here is my upload_form.php file:

<html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>

<?php echo $error;?>

<?php 
echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />
<
br /><br />
<
input type="file" name="userfile2" size="20" />
<
br /><br />
<
input type="file" name="userfile3" size="20" />
<
br /><br />
<
input type="file" name="userfile4" size="20" />
<
br /><br />
<
br /><br />

<
input type="submit" value="upload" />

</
form>

</
body>
</
html

Here is my upload_success.php file:

<html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>

<
h3>Your file was successfully uploaded!</h3>

<
ul>
<?php foreach($upload_data as $item => $value):?>
<li><?php echo $item;?><?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<? if ($upload_data['file_name']):?>

<? 
echo img('uploads/'.$upload_data['file_name']);?>

<? 
endif;?>

<p><?php echo anchor('upload''Upload Another File!'); ?></p>

</
body>
</
html
 Signature 

Leon Stafford

Media Temple loves me - use “leonstafford” at checkout for 15% off of all (gs), (ve) and (dv) server hosting plans!

 
Posted: 08 February 2009 09:19 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2009-02-07
52 posts

I had a bit of trouble with the last script, so here is one which is working much better now.

There are many examples of either M,V or C in these forums, but I couldn’t find one complete multi-uploader example which worked.

Hopefully this will save other CI beginners the hours I spent fiddling with this!

*haven’t checked the error functionality of this yet

upload.php Controller file:

<?php

class Upload extends Controller {
    
    
function Upload()
    
{
        parent
::Controller();
        
$this->load->helper(array('form''url','html'));
    
}
    
    
function index()
    
{    
        $this
->load->view('upload_form');
    
}

    
function do_upload()
    
{
        $config[
'upload_path''./uploads/';
        
$config['allowed_types''gif|jpg|png';
        
$config['max_size']    '3000';
        
$config['max_width']  '1024';
        
$config['max_height']  '768';
        
        
$this->load->library('upload'$config);
    
        if ( ! 
$this->upload->do_upload())
        
{
            $error 
= array('error' => $this->upload->display_errors());
            
            
$this->load->view('upload_form'$error);
        
}    
        
else
        
{
            $data 
= array('upload_data' => $this->upload->data());
            
            
//$this->load->view('upload_success', $data);
        
}
    }    
        
    
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
        
{
            
            $data 
= array('upload_data' => $files);
            
            
$this->load->view('upload_success'$data);
        
}
        
        
        
        
    } 
}
?> 

upload_form.php View file:

<html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>

<?php
        
if(isset($upload_message))
        
{
            
echo $upload_message;
        
}
    ?>

<?php 
echo form_open_multipart('upload/multiple_upload');?>

<input type="file" name="userfile1" size="20" />
<
br /><br />
<
input type="file" name="userfile2" size="20" />
<
br /><br />
<
input type="file" name="userfile3" size="20" />
<
br /><br />
<
input type="file" name="userfile4" size="20" />
<
br /><br />
<
br /><br />

<
input type="submit" value="upload" />

</
form>

</
body>
</
html

upload_success.php View file:

<html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>

<
h3>Your file was successfully uploaded!</h3>

<
ul>
<?php foreach($upload_data as $item => $value):?>

<?php
/*  EXAMPLE ARRAYS OUTPUT
[0] => Array   //access file_name with $value['file_name'] inside of <?php foreach($upload_data as $item => $value):?>
        (
            [file_name] => main9.jpeg
            [file_type] => image/jpeg
            [file_path] => E:/xampp/htdocs/igniter/uploads/
            [full_path] => E:/xampp/htdocs/igniter/uploads/main9.jpeg
            [raw_name] => main9
            [orig_name] => main.jpeg
            [file_ext] => .jpeg
            [file_size] => 4.58
            [is_image] => 1
            [image_width] => 160
            [image_height] => 90
            [image_type] => jpeg
            [image_size_str] => width="160" height="90"
    )

[1] => Array
    (
        [file_name] => Sunset.jpg
        [file_type] => image/jpeg
        [file_path] => E:/xampp/htdocs/igniter/uploads/
        [full_path] => E:/xampp/htdocs/igniter/uploads/Sunset.jpg
        [raw_name] => Sunset
        [orig_name] => Sunset.jpg
        [file_ext] => .jpg
        [file_size] => 69.52
        [is_image] => 1
        [image_width] => 800
        [image_height] => 600
        [image_type] => jpeg
        [image_size_str] => width="800" height="600"
    )
*/

?>

<li><?php echo $value['file_name']//outputs each files details as array?></li> 



<?php endforeach; ?>
</ul>






<
p><?php echo anchor('upload''Upload Another File!'); ?></p>

</
body>
</
html
 Signature 

Leon Stafford

Media Temple loves me - use “leonstafford” at checkout for 15% off of all (gs), (ve) and (dv) server hosting plans!

 
Posted: 09 February 2009 04:36 AM   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-02-07
52 posts

Here’s another example for people searching.

This one’s errors are working and the class is set up to be more modular for saving form results with multiple uploads and be re-useable for multiple forms by changing config arrays:

class file:

<?php
class Save extends Controller {
    
public $results = array();
    function 
Save()
    
{
        parent
::Controller();
        
$this->load->helper(array('form''url','html'));
        
$this->load->library('form_validation');
    
}
    
    
function index()
    
{    
        
        $this
->load->view('upload_form');    
    
}

    
function file_upload()
    
{
        
        $config[
'upload_path''./uploads/';
        
$config['allowed_types''gif|jpg|png';
        
$config['max_size']    '3000';
        
$config['max_width']  '1024';
        
$config['max_height']  '768';
        
$this->load->library('upload'$config);
        
$number_of_files 4;
        
        for (
$file_counter 0$file_counter $number_of_files$file_counter +=1)
            $this
->upload->initialize($config); 
            if ( ! 
$this->upload->do_upload('userfile'.$file_counter))
            
{
                $this
->results['result'.$file_counter]['error'= array($this->upload->display_errors('<p style="color:red;">''</p>'));
                
$this->results['result'.$file_counter]['success'= array('');
            
}    
            
else
            
{
                $this
->results['result'.$file_counter]['success'= array($this->upload->data());
                
$this->results['result'.$file_counter]['error'= array('');
            
}
        }
        
    }
    
public $validation_config = array();
    public 
$shop_validation = array(
               array(
                     
'field'   => 'username',
                     
'label'   => 'Username',
                     
'rules'   => 'trim|required|prep_for_form'
                  
),
               array(
                     
'field'   => 'password',
                     
'label'   => 'Password',
                     
'rules'   => 'trim|required|matches[passconf]|md5'
                  
),
               array(
                     
'field'   => 'passconf',
                     
'label'   => 'Password Confirmation',
                     
'rules'   => 'trim|required'
                  
),   
               array(
                     
'field'   => 'email',
                     
'label'   => 'Email',
                     
'rules'   => 'trim|required|valid_email'
                  
)
            );

    
    function 
validate_form()
    
{
        $this
->form_validation->set_error_delimiters('<p style="color:red;">''</p>');
        
$this->form_validation->set_rules($this->validation_config);        
                
        if (
$this->form_validation->run() == FALSE)
        
{
            $this
->load->view('upload_form'$this->results);
        
}
        
else
        
{
            $this
->load->view('form_success');
        
}
        
    }
    
function save_shop()
    
{
    $this
->file_upload();
    
$this->validation_config $this->shop_validation;
    
$this->validate_form();
    
}
}
?> 

view file:

<html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>
HTML code starts here


<?php
$total_files 
4;
?>

<?php 
$attributes 
= array('class' => 'email_form''id' => 'upload_form');
echo 
form_open_multipart('save/save_shop',$attributes);?>
<!-- add for do Y loop to set all image sections dynamically re number of file fields -->
<?php for ($x 0$x $total_files$x++){?>
<input type="file" name="userfile<?php echo $x; ?>" size="20" />
<
br />
<?php     if (isset(${"result$x"})){
            
if (${"result$x"}['error'][0]!=''){
                
//display error
                
echo ${"result$x"}['error'][0];
            
else {
                
echo ${"result$x"}['success'][0]['file_name'];
            
}
            
            
        }?>
<br /><br />
<?php } ?>
<h5>Username</h5>
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />

<
h5>Password</h5>
<?php echo form_error('password'); ?>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />

<
h5>Password Confirm</h5>
<?php echo form_error('passconf'); ?>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />

<
h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />


<!-- 
change array structure so that fields are always createdjust empty so can call directly without isset -->
<
br />

<
input type="submit" value="upload" />

</
form>

</
body>
</
html
 Signature 

Leon Stafford

Media Temple loves me - use “leonstafford” at checkout for 15% off of all (gs), (ve) and (dv) server hosting plans!

 
Posted: 29 March 2009 11:39 AM   [ # 13 ]   [ Rating: 0 ]
Joined: 2009-03-24
14 posts

Please see Single, Multiple and Multiple array upload library for a solution (hopefully soon)...

 
Posted: 13 April 2009 01:36 AM   [ # 14 ]   [ Rating: 0 ]
Joined: 2009-02-07
52 posts

Scrap my above workings,...

I found SWFupload to be much better at handling unlimited uploads while showing progress if needed.

I tested in a non-CI site first to make sure I understood how it worked, then read these forums and had it working pretty quickly.

+1 rating from me cheese

 Signature 

Leon Stafford

Media Temple loves me - use “leonstafford” at checkout for 15% off of all (gs), (ve) and (dv) server hosting plans!

 
Posted: 24 April 2009 12:24 PM   [ # 15 ]   [ Rating: 0 ]
Joined: 2008-06-02
13 posts

The original code not work for me, but here is my simplified working adaptation

class multiupload extends Controller
{

function index()
{
            $config[
'upload_path''./content/img/photos';
            
$config['allowed_types''gif|jpg|png';
            
$config['max_size']    '2048'//2 meg
            
$config['encrypt_name']    TRUE;
            
$this->load->library('upload'$config);
            
$this->load->library('image_lib');
            
//Upload error flag
            
$error FALSE;

            foreach(
$_FILES as $key => $value)
            
{
                
if( !empty($value['name']))
                
{
                    
if ( $this->upload->do_upload($key) )
                    
{
                        $uploaded 
$this->upload->data();

                        
//Creat Thumbnail
                        
$config['image_library''GD2';
                        
$config['source_image'$uploaded['full_path'];
                        
$config['create_thumb'TRUE;
                        
$config['thumb_marker''_tn';
                        
$config['master_dim''width';
                        
$config['quality'75;
                        
$config['maintain_ratio'TRUE;
                        
$config['width'175;
                        
$config['height'175;

                        
$this->image_lib->clear();
                        
$this->image_lib->initialize($config);
                        
$this->image_lib->resize();

                        
$imagename $uploaded['file_name'].'_tn'.$uploaded['file_ext'];
                        
$timestamp time();

                        
//Add Pic Info To Database
                        
$this->db->set('file'$imagename);
                        
//$this->db->set('date', $timestamp);

                        //Insert Info Into Database
                        
$this->db->insert('photos');
                    
}
                    
else
                    
{
                        $error 
TRUE;
                    
}
                }
            }
//If we have some error...
if($error$this->session->set_flashdata('notice''<div class="error icon"><ol>'.$this->upload->display_errors('<li>','</li>').'</ol></div>');

else 
$this->session->set_flashdata('notice''<p class="success icon">¡Success!</p>');

//Call the view
$this->load->view('upload_photos');
}

View file

<?php
echo $this->session->flashdata('notice');
echo 
'<h3>Upload multiple photos</h3>';
echo 
form_open_multipart('multiupload');
?>
  
<input type="file" name="p1" size="20" />
    <
input type="file" name="p2" size="20" />
    <
input type="file" name="p3" size="20" />
<
button type="submit" name="send-photos" id="send-photos">Send</button>
</
form

Enjoy and say ‘thaks’ if you find handy wink

Thanks!

 
1 of 3
1