我从我的表单上传了四张图片,但图片没有上传。我粘贴了下面的代码。请纠正我错误的地方。首先我做了验证然后我配置了和路径。后来我加载了上传库,后来为每张图片提供了图像路径。
我的控制器代码
public function upro()
{
$this->form_validation->set_rules('pro_name','Product','required');
$this->form_validation->set_rules('pro_image1','Image1','required');
$this->form_validation->set_rules('pro_image2','Image2','required');
$this->form_validation->set_rules('pro_image3','Image3','required');
$this->form_validation->set_rules('pro_image4','Image4','required');
// $today = date('Y-m-d');
if($this->form_validation->run()){
function uploadPic()
{
$config=[
'upload_path' => './uploads',
'allowed_types' => 'jpg|gif|png|jpeg'
];
$this->load->library('upload',$config);
}
$data = $this->input->post();
$today = date('Y-m-d');
$data['pro_date'] = $today;
$info = $this->upload->data();
$image_path = base_url("uploads/".$info['raw_name'].$info['file_ext']);
$data['pro_image1'] = $image_path;
$data['pro_image2'] = $image_path;
$data['pro_image3'] = $image_path;
$data['pro_image4'] = $image_path;
unset($data['submit']);
$this->adata->uproQ($data);
$this->session->set_flashdata('msg','Product uplaod success');
return redirect('admin/products');
}else{
$this->session->set_flashdata('msg','product uplaod failed');
return redirect('admin/apro');
}
}
我的模型代码
public function uproQ($data)
{
return $this->db->insert('products',$data);
}
我的表单视图代码
<?php echo form_open_multipart('admin/upro');?>
<label><h5>product Name:*</h5></label>
<?php echo form_input(['name'=>'pro_name','class'=>'form-control','placeholder'=>'product Name Here','value'=>set_value('pro_name')]);?>
<?php echo form_upload(['name'=>'pro_image1']);?>
<label><h5>product Image2:*</h5></label>
<?php echo form_upload(['name'=>'pro_image2']);?>
<label><h5>product Image3:*</h5></label>
<?php echo form_upload(['name'=>'pro_image3']);?>
<label><h5>product Image4:*</h5></label>
<?php echo form_upload(['name'=>'pro_image4']);?>
<button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button><hr>
<?php form_close();?>
答案 0 :(得分:1)
除了方法内部的方法,除了从不调用$this->upload->do_upload('name_of_input')
之外,上传类一次只能上传一个图像,你的文件数组需要一个for循环。此外,您无法对图片上传使用表单验证,表单验证仅适用于$_post
字段而非$_files
字段。我很惊讶这并没有给你一个错误,表明字段不存在。
$this->load->library('form_validation');
$this->form_validation->set_rules('pro_name', 'Product', 'required');
$expected_files = array('pro_image1', 'pro_image2', 'pro_image3', 'pro_image4');
//https://stackoverflow.com/questions/12289225/codeigniter-file-upload-required-validation
$i = 1;
foreach ($expected_files as $field_name) {
if (empty($_FILES[$field_name]['name'])) {
$this->form_validation->set_rules($field_name, 'Image' . $i, 'required');
}
$i++;
}
if ($this->form_validation->run()) {
$config = [
'upload_path' => './uploads',
'allowed_types' => 'jpg|gif|png|jpeg',
];
$this->load->library('upload', $config);
$data = array();
foreach ($_FILES as $field_name => $field_values) {
if (!in_array($field_name, $expected_files)) {
continue; // just in case user tries to add more upload fields
}
$this->upload->do_upload($field_name);
$info = $this->upload->data();
$image_path = base_url("uploads/" . $info['raw_name'] . $info['file_ext']);
$data[$field_name] = $image_path;
}
$today = date('Y-m-d');
$data['pro_date'] = $today;
$data['pro_name'] = $this->input->post('pro_name');
$this->adata->uproQ($data);
$this->session->set_flashdata('msg', 'Product upload success!');
return redirect('admin/products');
} else {
$this->session->set_flashdata('msg', validation_errors());
return redirect('admin/apro');
}