ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Latavish's Multiple Image Upload with Thumbnail Generation

May 27, 2008 10:41am

Subscribe [29]
  • #16 / Apr 24, 2009 12:24pm

    Santiag0

    10 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', '¡Success!');
    
    //Call the view
    $this->load->view('upload_photos');
    }
    }

    View file

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

    Enjoy and say ‘thaks’ if you find handy 😉

    Thanks!

  • #17 / Apr 24, 2009 6:14pm

    e-mike

    12 posts

    Santiag0, see my post for Single, Multiple and Multiple array upload library…

    http://ellislab.com/forums/viewthread/110130/

    This lib is an extended version which works the same way as the original but than also with multiple upload.

  • #18 / Apr 24, 2009 6:17pm

    Santiag0

    10 posts

    Wow, looks great, I will check it out. Thanks a lot Mike!

  • #19 / Jul 24, 2009 1:58pm

    cybercy

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

    Can you give us an example how you applied it, please?
    It would be great.

    Thank you

  • #20 / Aug 04, 2009 12:42pm

    Shanto

    12 posts

    Nice Script, Thanks for Sharing 😊

  • #21 / Nov 19, 2009 2:03am

    vik407

    15 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', '¡Success!');
    
    //Call the view
    $this->load->view('upload_photos');
    }
    }

    View file

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

    Enjoy and say ‘thaks’ if you find handy 😉

    Thanks!

    This code works great BUT must be correct the

    $imagename = $uploaded['file_name'].'_tn'.$uploaded['file_ext'];

    variable because produce duplicate extension (image.jpg_tn.jpg) in the saved image name.

    The little fix is:

    $imagename = substr($uploaded['file_name'], 0, -4)."_tn".$uploaded['file_ext'];

    i search for a better way to do the last code but im very very tired now so… if someone knows is time to share 😊

    Bye.

  • #22 / Nov 19, 2009 10:43am

    Latavish

    18 posts

    i totally forget about this post. I have a much better version of this script. i’ll post tonight when I get off the 9 to 5.

  • #23 / Nov 22, 2009 10:48am

    hugle

    289 posts

    i totally forget about this post. I have a much better version of this script. i’ll post tonight when I get off the 9 to 5.

    Hello mate, how is it going with an update?:)

    thanks!

  • #24 / Nov 23, 2009 12:41am

    meltingsand

    1 posts

    I’m also really anxious to see your latest version of your script. I have been working on integrating a cool flash-based solution into a non-CI site.  I would love to get that same type of interface working with CI.  It sounds like that is what you have working but I’m not sure.  I would also love to see a sample of how it looks/works.  Do you have anything up that we could look at?  Anyway thanks for your great contribution to the community

  • #25 / Jan 18, 2010 5:14am

    maria clara

    315 posts

    hi to all,

    im having an error with my upload script in CI. the image don’t show off in the database and in the folder where it should be put in.

    here’s my script:
    CONTROLLER:

    $config['upload_path'] = './reports/module/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']    = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $this->load->library('upload', $config);
                    
                    $file_data = $this->upload->data();
                    $file_name = $file_data['file_name'];
                    
                        $fields = array(
                    "module_id",
                                "module_name",
                    "slug",
                    "icon"=>$file_name,            
                                "report_file_name",
                                "sql_select",
                                "sql_filter",
                                "sql_order"
                                );
    
                        foreach ($fields as $field)
                        {
                            if (isset($_POST[$field])) $dt[$field] = $this->input->post($field);
                        }
                    
                        $item = $this->input->post("item");
                        $rows = $this->input->post("row0");
                                
                        $data['item'] = $this->Reports->save($dt,$item,$rows);

    MODEL:

    function save($data,$item,$details) {
            if ($item == '') {
                $data['link'] = "reports/view";
                $this->db->trans_start();
                $this->db->insert('sec_reports', $data);
                $item = $this->db->insert_id();
                $this->save_detail($item,$details);                
                $this->db->trans_complete();
            }
            else {
                $this->db->trans_start();
                $this->db->where('module_id', $item);
                $this->db->update('sec_reports', $data);
                $this->save_detail($item,$details);                
                $this->db->trans_complete();
            }
            return $item;
        }

    help me please..

    regards,
    kahtrina

  • #26 / Aug 04, 2010 1:49am

    littleflow3r

    1 posts

    very thankyou for santiag0 and vik407 😊

  • #27 / Sep 22, 2010 9:55am

    druid100

    11 posts

    @santiag0 ... nice work, it’s simple and works nearly flawlessly!

    but you have to call

    $this->upload->initialize($config);

    before each
           
    $this->upload->do_upload();

    to avoid problems caused by internal states of the upload class.

    @vik407

    the double extension problem is avoided if you add the code as mentioned above.

  • #28 / Jan 10, 2011 3:35pm

    SylaRbg

    2 posts

    Hi I have a form with 2 text boxes and multiple files. The problem is that when I have 2 attachments The method that saves information from the text boxes running 2 times
    (for each file).I know that the error comes from the cycle, but do not know how to fix it

    Example:

    FORM

    title = TEST
    desc = descript for test

    file1 = test.doc
    file2 = test2.doc

    DB
    id 1 TEST descript for test
    id 2 TEST descript for test

    Please help

    Comments are the Bulgarian

    foreach($_FILES as $key => $value)
               {
    
                             // Проверявам дали полетата са празни
                        if(!empty($value['name']))
                        {
                        $this->upload->initialize($config);
    
                        // АКО ИМА ГРЕШКИ
                    if (!$this->upload->do_upload($key))
                    {
                        $data['error'][$j] =  $this->upload->display_errors();
                        $j++;
                    }
                    else
                    {
                        $this->load->model('Process_image');
                        
                       // КАЧЕНИТЕ ФАЙЛОВЕ
                        $file = $this->upload->data();
                   
                    $file = array('upload_data' => $this->upload->data());
                   // Проверявам да ли файла е изображение
                    $name = $file['upload_data']['is_image'];
            
                    if(($name)){
                        // Ако е правя thumbnail
                     $this->Process_image->process_pic();
    
                    }else{
                        //Aко не е го записвам в базата
                        //$this->Art_model->add_f();
                        //$this->Art_model->add();
                       
                    }
    
                    }
                    }
                    $this->Art_model->add();
                    
                  
            }
  • #29 / Jul 14, 2011 6:40am

    maxrosecollins

    4 posts

    i totally forget about this post. I have a much better version of this script. i’ll post tonight when I get off the 9 to 5.

    did you ever post the update?

    Max

  • #30 / Jan 19, 2012 1:54pm

    mark.d

    17 posts

    Hi guys, im new to this and this thread helps me a lot..thank you!! 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases