Unable to upload any time of file Codeigniter

51 Views Asked by At

I am facing challenges on my upload code after moving to hmvc. If I set allowed_types=>'*' it works fine but if I set to jpg|jpeg or any type it echos message:

The filetype you are attempting to upload is not allowed

Below is my code:

public function upload()
{

  $customer_id = $this->session->userdata(user_id');
  $time_uploaded = date("Y-m-d H:i:s");
  $new_name = $string = str_replace(' ', '-', $time_uploaded);
  $directoryname = get_company_full_name($this->session->userdata('company_id')).'/payments';

  if (!is_dir(FCPATH.'customer_documents/'.$directoryname)) {
     mkdir(FCPATH.'customer_documents/' . $directoryname, 0777, TRUE);
    }

  $config = [
    'upload_path'             => FCPATH.'customer_documents/'.$directoryname,
    'allowed_types'         => 'jpg|png|pdf',
    'max_size'              => '1000000',
    'file_name'                 => $new_name,
    'overwrite'               => TRUE,
    'remove_spaces'         => TRUE,
    'encrypt_name'       => FALSE
  ];
  $this->load->library('upload');
  $this->upload->initialize($config);
  if(!$this->upload->do_upload()) {

     $errors = array('errors' => $this->upload->display_errors());
     $this->session->set_flashdata('errors',$errors['errors']);
     redirect('Payments/upload');

  }
  else
  {
      $file_data = $this->upload->data();
      $file_path = $new_name . $file_data['file_ext'];
      $file_path = $string = str_replace(' ', '-', $file_path);

      $time_uploaded = date("Y-m-d H:i:s");
      $append = random_string('alpha',5);
      $txn_id = strtoupper($append).'.'.random_string('nozero',7);

      $data  = array(
        'customer_id'       => $customer_id,
        'time_uploaded'     =>$time_uploaded,
        'proof_path'       => $file_path,
        'txn_id'            => $txn_id

      );

      //Insert upload into database
    $this->proof->insert_file($data);

    $name_of_org = get_company_full_name($customer_id);
    $data['org_name'] = $name_of_org;
    $data['txn_id'] = $txn_id;
    $data['page'] = 'uploaded_proof';

    //get email adresses for operating officers
    $email = $this->user->email();
      //load email library
    $this->load->library('email');

     $this->email->set_newline("\r\n");

    foreach ($email as $row) {
     //set email information and content
     $this->email->from('[email protected]', 'Ecofarmer');
     $this->email->to($row->email);
     $html_email = $this->load->view('email_view', $data, true);
     $this->email->subject(' Proof of Payment Upload ');
     $this->email->message($html_email);
     $this->email->send();

     $message = 'New Bank Payment from ' .$name_of_org. ', is awaiting approval.';
     send_message($message, $row->phone);
   }

   log_message('info', '***********************Customer with the ID: '.$this->session->userdata('company_id').' and name: '.get_company_full_name($this->session->userdata('company_id')).' uploaded proof of payment. The uploder phone number: '.$this->session->userdata('company_id').' The transaction ID: '.$txn_id.'***********************');

   //isset Message
   $this->session->set_flashdata('success', 'You have successfully uploaded your proof of payment. The Operation Officer will give you feedback in 2 hrs');

   //Redirect
   redirect('Payments/upload');


    }

    }

}
0

There are 0 best solutions below