Trushar Narodia
Trushar Narodia

Reputation: 366

upload multiple file in Codeigniter

i have one issue with multiple file uploading using codeigniter this is my view

<input type="file" name="song_name[]" value="" id="song_name">
<input type="file" name="song_name[]" value="" id="song_name">
<input type="file" name="song_name[]" value="" id="song_name">

this input element inside form with multipart/formdata

i am creating each textbox using jquery with same name this is my controller code for upload file

/* Conf */
$config['upload_path'] = './uploads/test/';
$config['allowed_types'] = 'gif|jpg|png|mov|mp3|aiff|mpeg|zip';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['file_ext_tolower'] = 'TRUE';
$config['remove_spaces'] = TRUE;

$this->load->library('upload', $config);
$filesCount = count($_FILES['song_name']['name']);
echo $filesCount;
print_r($_FILES['song_name']); exit;
for($i = 0; $i < $filesCount; $i++) {
    $_FILES['userFile']['name'] = $_FILES['song_name']['name'][$i];
    $_FILES['userFile']['type'] = $_FILES['song_name']['type'][$i];
    $_FILES['userFile']['tmp_name'] = $_FILES['song_name']['tmp_name'][$i];
    $_FILES['userFile']['error'] = $_FILES['song_name']['error'][$i];
    $_FILES['userFile']['size'] = $_FILES['song_name']['size'][$i];

    print_r($_FILES['userFile']);
    $this->upload->initialize($config);
    if($this->upload->do_upload('userFile')) {
        echo "upload";
        $fileData = $this->upload->data();
        $uploadData[$i]['file_name'] = $fileData['file_name'];
        $uploadData[$i]['created'] = date("Y-m-d H:i:s");
        $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
    } else {
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
    }
}

so please help me for this issue and let me know how to upload with this method

Upvotes: 1

Views: 1615

Answers (2)

Satish Kumar
Satish Kumar

Reputation: 1

public function fileUpload()
    {

                    $config['upload_path'] = 'uploads/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '2048';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    
                    $this->load->library('upload', $config);

                    if (empty($this->upload->do_upload($_FILES['image']['name'])))
                    {
                        print_r($_FILES['image']['name']);die;
                        $this->session->set_flashdata('error','Error'); 
                        // redirect('upload','refresh');
                        echo "error";
                        // $this->load->view('upload_form', $error);
                    }
                    else
                    {
                        
                        $data = array('upload_data' => $this->upload->data());
                        print_r($data);die;
                        $this->session->set_flashdata('success','Sucess');
                        redirect('upload','refresh');
                        // $this->load->view('upload_success', $data);
                    }
        

    }

Note:error please solve

Upvotes: 0

Var  Yan
Var Yan

Reputation: 96

/**
 * @param array $config
 * @param string $file_input_name
 * @param array $resize_configs
 * @return array
 * */
protected function multiple_upload($config = array(),$file_input_name,$resize_configs = null)
{
    $fileNames = array();

    $conf['upload_path'] = FCPATH . 'assets/img';
    $conf['allowed_types'] = 'jpg|png|jpeg';
    $conf['max_size'] = 50000;
    $conf['max_width'] = 3600;
    $conf['max_height'] = 1800;
    $conf['maintain_ratio'] = TRUE;
    $conf['encrypt_name'] = TRUE;

    foreach ($config as $item => $val){
        $conf[$item] = $val;
    }

    $this->load->library('upload',$conf);
    $this->load->library('image_lib');
    $files = $_FILES;
    $cpt = sizeof($_FILES[$file_input_name]['name']);
    for($i = 0; $i < $cpt; $i++)
    {
        $_FILES['userfile']['name']     = $files[$file_input_name]['name'][$i];
        $_FILES['userfile']['type']     = $files[$file_input_name]['type'][$i];
        $_FILES['userfile']['tmp_name'] = $files[$file_input_name]['tmp_name'][$i];
        $_FILES['userfile']['error']    = $files[$file_input_name]['error'][$i];
        $_FILES['userfile']['size']     = $files[$file_input_name]['size'][$i];

        if($this->upload->do_upload()){
            $fileNames[] = $this->upload->file_name;
            if(!is_null($resize_configs)){
                //Image Resizing
                $img_config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                foreach($resize_configs as $key => $value){
                    $img_config[$key] = $value;
                }

                $this->image_lib->initialize($img_config);
                $this->image_lib->resize();
            }
        }
    }

    return $fileNames;

}

Upvotes: 1

Related Questions