我找到了一些使上传可选的例子,但到目前为止它对我没用。我可能错过了一些东西,所以我希望有人能抓住它?这是我所关注的链接
https://blog.smalldo.gs/2013/03/optional-file-upload-field-codeigniter/
到目前为止,我可以编辑产品,如果我没有上传文件但是如果我上传文件,我得到的是你没有选择要上传的文件错误。请帮忙。谢谢。
控制器
public function editProduct(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputproductname', 'Name', 'trim|required');
$this->form_validation->set_rules('inputproductdescription', 'Description', 'trim|required');
$this->form_validation->set_rules('inputproductprice', 'Price', 'trim|required');
$inputproductname = $this->input->post('inputproductname');
$inputproductdescription = $this->input->post('inputproductdescription');
$inputproductprice = $this->input->post('inputproductprice');
$inputdateadded = date('Y-m-d');
$inputcurrentproductid = $this->input->post('inputcurrentproductid');
$inputcurrentproductstatus = $this->input->post('inputcurrentproductstatus');
$config['upload_path'] = $this->getProductImageFolderPath();
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 3000;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = $inputproductname;
$this->load->library('upload', $config);
if($this->form_validation->run()==false){
$data['product'] = $this->ProductsModel->getProduct($inputcurrentproductid);
$data['edit'] = "true";
$data['message']='';
$data['inputcurrentproductid'] = $inputcurrentproductid;
$data['inputcurrentproductstatus'] = $inputcurrentproductstatus;
$this->load->view('control/controlMenu/navigationLink');
$this->load->view('control/controlProducts/productDetail',$data);
$this->load->view('control/controlMenu/navigationJquery');
}else{
if(isset($_FILES['upload'])&&$_FILES['upload']['size']>0){
if(!$this->upload->do_upload()){
$this->session->set_flashdata('Form',$this->upload->display_errors());
redirect('Control/ProductDetail/'.$inputcurrentproductid."/".$inputcurrentproductstatus);
}else{
$extension = $this->upload->data('file_ext');
$productdetails = array(
'name'=>$inputproductname,
'description'=>$inputproductdescription,
'price'=>$inputproductprice,
'imagePath'=>$config['upload_path'].$config['file_name'].$extension,
'dateAdded'=>$inputdateadded
);
$this->db->trans_start();
$this->ProductsModel->editProduct($inputcurrentproductid,$productdetails);
$error = $this->db->error();
$this->db->trans_complete();
if($this->db->trans_status()===false){
}else{
$this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
redirect('/Control/Products');
}
if($error!=''){
$this->session->set_flashdata('Form',$error["message"]);
redirect('/Control/Products');
}
}
}else{
$productdetails = array(
'name'=>$inputproductname,
'description'=>$inputproductdescription,
'price'=>$inputproductprice
);
$this->db->trans_start();
$this->ProductsModel->editProduct($inputcurrentproductid,$productdetails);
$error = $this->db->error();
$this->db->trans_complete();
if($this->db->trans_status()===false){
}else{
$this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
redirect('/Control/Products');
}
if($error!=''){
$this->session->set_flashdata('Form',$error["message"]);
redirect('/Control/Products');
}
}
}
答案 0 :(得分:0)
您没有告诉上传库从哪个字段获取文件。
$this->upload->do_upload() // <-- no argument for this method provided
如果没有提供字段名称,默认情况下它会尝试从userfile
字段[docs]获取文件,但您的文件输入称为upload
,如代码示例中所示。所以你应该做
if(!$this->upload->do_upload("upload")){
另请注意,在客户端,带有文件字段的表单应声明为
<form method="post" action="some_action" enctype="multipart/form-data" />
否则文件字段也将为空。