I want to upload files after the successful insertion of some data into the database but the file uploading does not work.
The following is my code for file upload but it's not uploading.
Controller
public function saveReceipt(){
$doc=$this->receipt_m->saveReceipt_m();
if($doc){
$countfiles = count($_FILES['attatchments']['name']);
for($i=0;$i<$countfiles;$i++){
if(!empty($_FILES['attatchments']['name'][$i])){
// Define new $_FILES array - $_FILES['file']
$_FILES['file']['name'] = $_FILES['attatchments']['name'][$i];
$_FILES['file']['type'] = $_FILES['attatchments']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['attatchments']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['attatchments']['error'][$i];
$_FILES['file']['size'] = $_FILES['attatchments']['size'][$i];
// Set preference
$config['upload_path'] = base_url("assets/attachments");
$config['allowed_types'] = 'pdf|txt';
$config['max_size'] = '5000'; // max_size in kb
$config['file_name'] = $_FILES['attatchments']['name'][$i];
//Load upload library
$this->load->library('upload',$config);
$arr = array('msg' => 'something went wrong', 'success' => false);
// File upload
if($this->upload->do_upload('file')){
$data = $this->upload->data();
$arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);
}
}
}
echo json_encode($arr);
}else{
$response=array("status"=>false);
}
}
View
<form class="horizontal-form" name="frmSaveReceipt" id="frmSaveReceipt" enctype="multipart/form-data">
<input type="file" name="attatchments[]" id="file" multiple="multiple">
</form
"status" false? that means $doc doesn't return anything, which means you need to analyze in your modelreceipt_mthe functionsaveReceipt_m(), please add the relevant code...