1

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

1
  • thanks @manoj Sharma now it working Commented Jan 5, 2017 at 11:17

2 Answers 2

1
/**
 * @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;

}
Sign up to request clarification or add additional context in comments.

5 Comments

This function working prefect for my application just change allowed types to types you need and correct other configurations.
yes buddy now it working after did some change in your code as per my requirement
i have one question how to setup validation for input file for name="song_name[]"
what kind of validation you need?
for file input which one given in question like for song_name[] its working for song_name but not for song_name[]
0
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

2 Comments

Please add further details to expand on your answer, such as working code or documentation citations.
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.