0

I want to upload each file with multiple insert_id but not happening. I don't know why? I know its not a hard question for all but for me!

   public function do_upload($act_id)
   {                     
    $config['upload_path'] = "../public/assets/images/documents/";
    $config['allowed_types'] = 'jpg|jpeg|gif|png|zip|docx|pdf|txt';
    $config['max_size'] = '100000';
    //$config['max_width'] = '1024';
    //$config['max_height'] = '768';

    foreach($act_id as $id){

        foreach($_FILES['add_file']['name'] as $fileName){

    $ext = end(explode(".", $fileName));

    $config['file_name'] = $id.'.'.$ext;
        }       
    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    $imageName = $config['upload_path'].$id.'.'.$ext;   
    unlink($imageName);

    if(!$this->upload->do_upload('add_file'))
    {
        $error = array('error'=>$this->upload->display_errors());
        $this->load->view('disciplinary_action_register/disciplinary_action_register_form',$error);
    }               
    else
    {
        $data = array('upload_data'=>$this->upload->data());                                            
        $pictureName = array(
            'FILE_DETAILS' => $id.'.'.$ext  
        );

        $this->db->update('lib_disciplinary_action_register', $pictureName, array('DISIPLINARY_ACTION_REGISTER_ID' => $id));
    }
    }
}
2
  • By but not happening. I don't know why? what do you mean ? did you got an error ? Commented Feb 10, 2016 at 6:34
  • no error dispalying ! Commented Feb 10, 2016 at 6:51

1 Answer 1

1

Use this if helps

function do_upload($act_id)
    {       
        $file_ary       = $this->reArrayFiles($_FILES['add_file']);
        $allowed        = array('png', 'jpg', 'gif','jpeg','gif','zip','docx','pdf','txt');
        $gal            = "../public/assets/images/documents/";
        foreach($act_id as $id)
        {   
            foreach ($file_ary as $file)
            {
                $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
                if(!in_array(strtolower($extension), $allowed))
                {
                    continue;// or collect error if you  want       
                }           
                $uniquename = unique();//generates a unique name to avoid override 
                $fully      = $uniquename.'.'.$extension;                       
                if(move_uploaded_file($file['tmp_name'], $gal.'/'.$fully))
                {
                     $pictureName = array('FILE_DETAILS' => $fully  );
                     $this->db->update('lib_disciplinary_action_register', $pictureName, array('DISIPLINARY_ACTION_REGISTER_ID' => $id));
                }                   
            }
        }
    }
    function reArrayFiles(&$file_post) 
    {
        $file_ary = array();
        $file_count = count($file_post['name']);
        $file_keys = array_keys($file_post);
        for ($i=0; $i<$file_count; $i++) 
        {
            foreach ($file_keys as $key) 
            {
                $file_ary[$i][$key] = $file_post[$key][$i];
            }
        }

        return $file_ary;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

you changing the array into string $_FILES['add_file']['name']= $files['add_file']['name'][$i]; so the multiple upload won't happen only single file upload only happens
its working but not properly as required ! its uploading each file with one id like ( 7.docx ,7.jpg). if i replace unique id by id .
@M.Alim Updated again check now. And use as it is please.

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.