0

I have form with 4 file fields and I want to upload these files properly.

My code is as follows :

public function do_upload(){
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 100;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;

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

    if ( ! $this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    } else {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

enter image description here

Littm
  • 4,923
  • 4
  • 30
  • 38
Muhammad Saim
  • 63
  • 1
  • 8
  • I wrote an answer to a similar question the other day; Maybe this will help - https://stackoverflow.com/questions/62373664/adding-timestamp-while-uploading-image-file-is-not-working/62374346#62374346 – sauhardnc Jun 17 '20 at 05:35
  • What's your question about this? Can you explain what **exactly** is not working with the given code, and what you've tried to resolve this? – Nico Haase Jun 17 '20 at 06:27

1 Answers1

0
private function fileUpload($name, $config = []){
     $config['upload_path']          = './uploads/';
     $config['encrypt_name']         = TRUE;
     $config['file_ext_tolower']     = TRUE;
     $config['allowed_types']        = 'jpg|jpeg|png';
     $this->upload->initialize($config);
     if ( ! $this->upload->do_upload($name))
     {
         $error = array('error' => true, 'message' => $this->upload->display_errors());
         return $error;
     }
     else
     {
         return array('error' => false, 'upload_data' => $this->upload->data());
     }
}

Create this private function in the controller just call this function when you have to upload the file with the name of the file field if you want some other config just pass config array to as a next parameter

$data = $this->fileUpload('site_logo_dark')
if($data['error']){
  echo data['message']; // error message while uplaoding an file if have any
}else{
  echo $data['upload_data']['file_name']; // return you a name of the file which you just upload it you can save it to the database 
}
Muhammad Saim
  • 63
  • 1
  • 8