我尝试通过stackoverflow中的codeigniter文档和示例来查找此问题的解决方案,但它没有帮助。这是我上传图片的代码。 我也通过自动加载使用上传库。
这是我在我的控制器中的功能:
public function do_upload()
{
$config['upload_path'] = './assets/dorm_images';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload("userfile");
}
这是我提交的表格:
<?php $attributes = array('id' => 'listing_form'); echo form_open_multipart('dorm/list_dorm',$attributes);?>
<input type="text" name='name' placeholder='Name'><br>
<input type="text" name='description' placeholder='Description'><br>
<input type="text" name='price' placeholder='Price ($)'><br>
<input type="file" name="userfile" class='btn btn-success btn-mini' size="20" /><br>
<input type="submit" value='List this Item' class='btn btn-warning' id='submit'>
</form>
这是在表单提交时触发的函数:
public function list_dorm(){
$this->check_logged_in();
$this->load->library('form_validation');
$this->form_validation->set_rules('name','Name', 'required');
$this->form_validation->set_rules('condition','condition', 'required');
$this->form_validation->set_rules('category','condition', 'required');
$this->form_validation->set_rules('price','price', 'required|is_natural_no_zero');
$this->form_validation->set_message('is_natural_no_zero', 'The price field must be a whole number.');
$data=array();
$view_data=array();
$this->load->model('dorm_model');
if($this->form_validation->run() === FALSE ){
$errors=validation_errors();
$data['insert_errors']='<div class="alert alert-danger"><p>'.$errors.'</p></div>';
}
else{
$user_session=$this->session->userdata('user_session');
$user_id=$user_session['id'];
$data['user_id']=$user_id;
$data['name']=$this->input->post('name');
$data['image_path']='/assets/dorm_images/x.jpg';
$data['description']=$this->input->post('description');
$data['price']=$this->input->post('price');
$data['created_at']=date("Y-m-d H:i:s");
$this->do_upload();
$this->load->model('dorm_model');
$this->dorm_model->list_dorm($data);
$data['success']='"'.$data['name'].'" is now listed for sale.';
}
echo json_encode($data);
}