我在这里添加附加文件输入add_incident表单,但提交表单后,文件名未保存在数据库中,文件也未保存在上传文件夹中。上传文件夹路径正确。运行
echo "<pre>";
print_r($_FILES);
die;
输出是:
数组((文件名)=比;数组([名字]=比;mot-testing-station 2.每股收益[型]=比;应用程序/后记[tmp_name] =比;/opt/lampp/temp/phpMSzQT6(错误)=比;0(大小)=比;405159()
但是它没有给出任何输出,它只是继续提交表单并创建新的事件。
谁能帮我一下我错过了什么?
view.php
<h2 class="heading">Add Incidents</h2>
<?php echo form_open_multipart('incidents_add'); ?>
<div class="row">
<div class="form-group col-md-4">
<label for="email">Incidents ID</label>
<input type="text" class="form-control" id="T_id" placeholder="Name" name="T_id"value="<?php echo $T_id;?>" readonly >
</div>
<div >
<input id = "choose_image" type="file" name="filename" size="20" />
</div>
<div class="col-md-12">
<div class="btn-section float-right">
<button class="btn btn-outline-primary" value="Create"><i class="fa fa-plus-circle" aria-hidden="true"></i> Create</button>
</div>
</div>
</div>
</form>
controller.php
function add()
{
$existing_count = $this->incidents_model->get_task_count();
$data['T_id'] = 'T'.sprintf("%'.03dn",$existing_count+1);
$this->form_validation->set_rules('Status', 'Status ', 'required');
$this->form_validation->set_rules('Priority', 'Priority' , 'required');
$this->form_validation->set_rules('Description', 'Description' , 'required');
$data['incidents'] = $this->incidents_model->getIncidentsDetails();
$data['T_id'] = $this->incidents_model->get_task_count();
$data['company_name'] = $this->incidents_model->getAllCompanyName();
if (isset($_FILES['filename'])){
// print_r($_FILES);
// die;
$file_name = time().'_'.$_FILES['filename']['name'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|dox|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['file_name'] = $file_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('filename'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/incidents/add', $error);
}
else
{
$data = array('file' =>$this->upload->data());
$this->load->view('admin/incidents/add', $data);
}
}
if ($this->form_validation->run() ==true)
{
$this->incidents_model->add();
$this->session->set_flashdata ('success','Incidents Added Sucessfully');
redirect('admin/incidents/index' ,$data);
}
else{
$this->load->view('admin/incidents/add', $data);
}
}
所以我是如何解决这个问题的,我添加了
$data['file'] = $this->incidents_model->add($image_name);
,这就解决了我的问题。
控制器:
function add()
{
$existing_count = $this->incidents_model->get_task_count();
$data['T_id'] = 'T'.sprintf("%'.03dn",$existing_count+1);
$this->form_validation->set_rules('T_id', 'Tasks Id', 'required');
$this->form_validation->set_rules('Tname', 'Tasks Name' , 'required');
$this->form_validation->set_rules('Cname', 'Company Name ', 'required');
$this->form_validation->set_rules('Pname', 'Project Name' , 'required');
$this->form_validation->set_rules('Pversion', 'Project Version', 'required');
$this->form_validation->set_rules('Rform', 'Report Form ', 'required');
$this->form_validation->set_rules('Internal', 'Internal ', 'required');
$this->form_validation->set_rules('Assignto', 'Assign To' , 'required');
$this->form_validation->set_rules('Sdate', 'Start Date', 'required');
$this->form_validation->set_rules('Edate', 'End Date' , 'required');
$this->form_validation->set_rules('Status', 'Status ', 'required');
$this->form_validation->set_rules('Priority', 'Priority' , 'required');
$this->form_validation->set_rules('Description', 'Description' , 'required');
$data['incidents'] = $this->incidents_model->getIncidentsDetails();
$data['T_id'] = $this->incidents_model->get_task_count();
$data['company_name'] = $this->incidents_model->getAllCompanyName();
if ($this->form_validation->run() ==true)
{
if (isset($_FILES['filename']))
{
// echo "<pre>";
// print_r($_FILES);
// die;
$file_name = time().'_'.$_FILES['filename']['name'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|dox|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = TRUE;
$config['file_name'] = $file_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('filename'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/incidents/add', $error);
}
else
{
$image_data = $this->upload->data();
$image_name = $image_data['file_name'];
$data =array(
'file' => $image_data['file_name']
);
$data['file'] = $this->incidents_model->add($image_name);
}
}
$this->session->set_flashdata ('success','Incidents Added Sucessfully');
redirect('admin/incidents/index' ,$data);
}
else{
$this->load->view('admin/incidents/add', $data);
}
}
并修改了模型中的add函数,如下所示:
function add()
{
$arr['incidents_id'] = $this->input->post('T_id');
$arr['incident_name'] = $this->input->post('Tname');
$arr['company_name'] = $this->input->post('Cname');
$arr['project_name'] = $this->input->post('Pname');
$arr['project_version'] = $this->input->post('Pversion');
$arr['report_form'] = $this->input->post('Rform');
$arr['internal'] = $this->input->post('Internal');
$arr['assign_to'] = $this->input->post('Assignto');
$arr['start_date'] = $this->input->post('Sdate');
$arr['end_date'] = $this->input->post('Edate');
$arr['status'] = $this->input->post('Status');
$arr['priority'] = $this->input->post('Priority');
$arr['description'] = $this->input->post('Description');
$arr['initial_status'] = 0 ;
$arr['flag'] = 1;
$arr['file'] = time().'_'.$_FILES['filename']['name'];
$this->db->insert('incidents',$arr);
$insert_id = $this->db->insert_id();
$arr1['incident_id'] = $insert_id;
$arr1['incident_status'] = $this->input->post('Status');
$arr1['incident_description'] = $this->input->post('Description');
$this->db->insert('incident_status',$arr1);
}