简体   繁体   中英

Multiple Images not uploading Only Last Select Image Uploaded

I am uploading multiple images in codeigniter framework. When i select multiple images, it upload only last selected image.

Below is my controller code, where i am doing wrong?

$files = $_FILES;
        for($i=0; $i< count($_FILES['item_image']['name']); $i++)
        {           
            $_FILES['item_image']['name']= $files['item_image']['name'][$i];
            $_FILES['item_image']['type']= $files['item_image']['type'][$i];
            $_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$i];
            $_FILES['item_image']['error']= $files['item_image']['error'][$i];
            $_FILES['item_image']['size']= $files['item_image']['size'][$i];    

            $this->upload->initialize($config);
            if ($this->upload->do_upload('item_image'))
            {
                $file_name=$this->upload->data('file_name');
                $newdata = array(
                    'item_id'=>$last_id,
                    'image_name'=>$file_name
                );
                $this->Item_model->add_item_image($newdata);
                $newdatatwo = array(
                    'item_image'=>$file_name
                );
                    $this->Item_model->update_item($newdatatwo,$last_id);
                     $data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Item added Successfully!');

            }else{

                $data['error_or_success_message'] = $this->session->set_flashdata('error_or_success_message', 'Some Error, Please Try Again!');
            }
        }

The second condition in your for loop gets redefined in the first line of your for loop. I would check to make sure that isn't causing errors.

Refer The Below Code

Controller Code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Multiple_upload extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
    }

    public function index()
    {
        $this->load->view('upload');
    }

    public function Upload()
    {
        $files = $_FILES;
        $config['upload_path']  = 'common_assets/images';
        $config['allowed_types']    = 'jpg|gif|png';
        $config['max_size']         = '';
        $config['remove_spaces']    = true;
        $config['overwrite']        = true;
        $config['max_width']        = '';
        $config['max_height']       = '';
        $config['max_filename']     = 0;    
        foreach ($_FILES['item_image']['name'] as $key => $value)
        {
            $_FILES['item_image']['name']= $files['item_image']['name'][$key];
            $_FILES['item_image']['type']= $files['item_image']['type'][$key];
            $_FILES['item_image']['tmp_name']= $files['item_image']['tmp_name'][$key];
            $_FILES['item_image']['error']= $files['item_image']['error'][$key];
            $_FILES['item_image']['size']= $files['item_image']['size'][$key];  
            $this->load->library('upload', $config);
            $this->upload->initialize($config); 
            if (!empty($value)) 
            { 
                if (!$this->upload->do_upload('item_image'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    print_r($error);
                    exit();
                }else{
                    $file_name=$this->upload->data('file_name');
                    //**********Database Entry*********************
                    // $newdata = array(
                    //     'item_id'=>$last_id,
                    //     'image_name'=>$file_name
                    // );
                    // $this->Item_model->add_item_image($newdata);
                    // $newdatatwo = array(
                    //     'item_image'=>$file_name
                    // );
                    // $this->Item_model->update_item($newdatatwo,$last_id);
                }
            }
        }
        echo "<pre>Uploaded Successfully"; die;
    }
} 

?>

//This works for me:
$config['upload_path']  = 'common_assets/images';
$config['allowed_types']    = 'jpg|gif|png';
$config['max_size']         = '';
$config['remove_spaces']    = true;
$config['overwrite']        = true;
$config['max_width']        = '';
$config['max_height']       = '';
$config['max_filename']     = 0;  

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

foreach ($_FILES as $fieldname => $fileObject) {
    if (!empty($fileObject['name'])) {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($fieldname)) {
            $errors = $this->upload->display_errors();
            redirect('error_page', $errors);
        } else {
            $data = array($fieldname => $this->upload->data());
            extract($data[$fieldname]);
            $$fieldname = $file_name;
        }
    }
}

$array_to_save = array('tbl_name_field_1' => $file_name_from_post_1, 
                       'tbl_name_field_2' => $file_name_from_post_2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM