我是codeigniter的新手,任何人都可以帮我上传多个图片。方案是,验证每个图像然后是否有错误,在每个输入文件下面显示每条消息。如果没有错误上传并将文件名保存到数据库。我真的需要一个基本的方法来解决这个问题。我尝试了什么,但没有运气。很感谢任何形式的帮助。提前谢谢!
这是我的控制器
hotel.php
public function add()
{
//if(!is_ajax_request()) return;
$this->set_validation_rules();
if($this->form_validation->run())
{
$m_insert = $this->get_posted_hotel_data();
$hotel_id = $this->hotel_model->insert($m_insert);
$this->upload_image($hotel_id);
redirect('hotel');
}
else
{
$data['accept'] = array( 0 => 'False', 1 => 'True');
$data['destinations'] = prepare_dropdown_array($this->destination_model->get(), 'cde_id', 'cde_name', '--please select');
echo $this->load->view('form_hotel_add', $data);
}
}
private function set_validation_rules()
{
$this->form_validation->set_rules('destination', '', 'required');
$this->form_validation->set_rules('stars', '', 'required|numeric');
$this->form_validation->set_rules('name', '', 'required');
$this->form_validation->set_rules('address', '', 'required');
$this->form_validation->set_rules('email', '', 'required|valid_email');
$this->form_validation->set_rules('phone', '', 'required|numeric');
$this->form_validation->set_rules('tm_comments', '', 'required');
$this->form_validation->set_rules('am_comments', '', 'required');
$this->form_validation->set_rules('accept[]', '', 'required');
$this->form_validation->set_rules('room', '', 'required');
$this->form_validation->set_rules('location', '', 'required');
$this->form_validation->set_rules('amenities', '', 'required');
$this->form_validation->set_rules('image1', 'Image1', 'callback__handle_upload');
$this->form_validation->set_rules('image2', 'Image2', 'callback__handle_upload');
$this->form_validation->set_rules('image3', 'Image3', 'callback__handle_upload');
$this->form_validation->set_message('required', 'This field is required.');
}
private function get_posted_hotel_data()
{
$data1 = array('cho_destination' => $this->input->post('destination'),
'cho_stars' => $this->input->post('stars'),
'cho_name' => $this->input->post('name'),
'cho_address' => $this->input->post('address'),
'cho_email' => $this->input->post('email'),
'cho_phone' => $this->input->post('phone'),
'cho_tm_comment' => $this->input->post('tm_comments'),
'cho_am_comment' => $this->input->post('am_comments'),
'cho_rooms' => $this->input->post('room'),
'cho_location' => $this->input->post('location'),
'cho_amenities' => $this->input->post('amenities'));
foreach($_POST['accept'] as $v)
{
$data2 = array("cho_accept" => $v);
}
$data = array_merge($data1, $data2);
return $data;
}
private function upload_image($id)
{
$ctr=1;
$x=1;
foreach($_FILES as $key => $value)
{
//print_r($_FILES);
//die();
if(!empty($value['name']))
{
$config['upload_path'] = './uploads/hotels';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['encrypt_name'] = true;
//$config['file_name'] = sprintf('%s_%s', $id, $value['name']);
$filename=explode('.', $value['name']);
$config['file_name'] = sprintf('%s_%s', $id, 'image_'.$ctr.'.'.$filename[1]);
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(move_uploaded_file($_FILES["image".$x]["tmp_name"],"uploads/hotels/" . $config['file_name']))
{
$uploaded = $this->upload->data();
//Create Thumbnail
$img_lib['image_library'] = 'gd2';
$img_lib['source_image'] = $uploaded['full_path'];
$img_lib['master_dim'] = 'width';
$img_lib['quality'] = 75;
$img_lib['maintain_ratio'] = TRUE;
$img_lib['width'] = 380;
$img_lib['height'] = 280;
$this->load->library('image_lib', $img_lib);
$this->image_lib->clear();
$this->image_lib->initialize($img_lib);
$this->image_lib->resize();
}
}
if(!empty($_FILES['image'.$x]['tmp_name']))
{
$this->add_image($id, $config['file_name'], $x);
}
$ctr++;
$x++;
}
}
public function _handle_upload()
{
$y=1;
foreach($_FILES as $key=> $val)
{
if(empty($_FILES['image'.$y]))
{
$this->form_validation->set_message('_handle_upload', "You must upload an image!");
return false;
}
$y++;
}
}
private function add_image($id, $image, $x)
{
$data = array('cho_image'.$x => $image);
$this->hotel_model->update($id, $data);
}
这是我的观点: form_hotel_add.php
<div class="control-group">
<label for="image1" class="control-label">Image 1</label>
<div class="controls">
<input type="file" id="image1" name="image1" accept="image/*" />
<?=form_error('image1', '<br><label class="error">','</label>')?>
</div>
</div>
<div class="control-group">
<label for="image2" class="control-label">Image 2</label>
<div class="controls">
<input type="file" id="image2" name="image2" accept="image/*"/>
<?=form_error('image2', '<br><label class="error">','</label>')?>
</div>
</div>
<div class="control-group">
<label for="image3" class="control-label">Image 3</label>
<div class="controls">
<input type="file" id="image3" name="image3" accept="image/*" />
<?=form_error('image3', '<br><label class="error">','</label>')?>
</div>
</div>
答案 0 :(得分:0)
你不需要使用move_upload_file() 在codeigniter的上传类中使用do_upload()方法