我正在尝试在codeigniter 3上进行图像上载验证。我尝试了许多tut,但仍然遇到此问题。我希望用户将上传的照片区域留空(最终如果他们上传不受支持的mime和大小),以便在允许或尝试提交数据之前引发错误。
现在它会引发错误,但是显示数据库查询以及“ sql中的图像不能为空”。我希望它能像常规表单验证一样显示,而不会显示sql查询和错误。
它似乎仍在提交表单时没有先抛出错误,因为它说 $ post_image未定义。
我已经尝试使用isset
函数输入回调,以尝试仅允许表单在上传图像时提交数据,否则,我试图通过传递error变量来显示错误。正如我上面所解释的,所有这些似乎都无法正常工作。
控制器:
public function create(){
//Check in login session
if(!$this->session->userdata('logged_in')){
$this->session->set_flashdata('log_post','Please login or create a free account to post a ad.');
redirect('users/register');
}
$data['title'] = 'Create a Post';
$data['categories'] = $this->post_model->get_categories();
$data['states'] = $this->post_model->get_city();
$this->form_validation->set_error_delimiters('<div class="error"> <h7> Error: </h7>', '</div>');
$this->form_validation->set_rules('title','Title',array('required', 'min_length[3]'));
//$this->form_validation->set_rules('file','Image Upload','required');
$this->form_validation->set_rules('Description','About You',array('required', 'min_length[5]'));
$this->form_validation->set_rules('Number','Phone Number',array('required', 'min_length[7]'));
$this->form_validation->set_rules('Area','Location/Area',array('required', 'min_length[2]'));
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// $this->load->helper('file');
//$this->form_validation->set_rules('file','','callback_file_check');
if($this->form_validation->run()==TRUE){
$config['upload_path'] = 'assets/images/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['encrypt_name'] = TRUE; //TURN ON
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$errors = array('error'=>$this->upload->display_errors());
$this->load->view('templates/header');
$this->load->view('posts/create', $errors);
$this->load->view('templates/footer');
}else {
$data = $this->upload->data();
$post_image = $data['file_name'];
}
}
$this->post_model->create_post($post_image);
$this->session->set_flashdata('post_created','Your Post has been submitted');
redirect('posts');
}
}
}// end of class
型号:
public function create_post($post_image){
$slug = md5(uniqid().mt_rand());
//url_title($this->input->post('title'), 'dash', TRUE). // this is the orginal way for slug SEO friendly
$site = $this->input->post('site');
//adds HTTP too website links
if (!preg_match("~^(?:f|ht)tps?://~i", $site)) {
$site = "http://" . $site;
}
$data = array(
'title'=>$this->input->post('title'),
'body'=> $this->input->post('Description'),
'post_image' => $post_image
);
return $this->db->insert('posts',$data);
}
查看:
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="textarea">Photo</label>
<div class="col-lg-8">
<div class="mb10">
<?php echo form_error('file') ?>
<input name="file" type="file" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
</div>
<?php if (isset($error)) { echo $error; } ?>
</div>