Php 图像上载仍在提交,并在CI中进行回调验证
我正在尝试在codeigniter 3上进行图像上传验证。我尝试了很多图坦卡蒙,但仍然面临这个问题。我想,如果用户将上传照片区域留空(最终,如果他们上传不支持的mime和大小),则在允许或尝试提交数据之前抛出错误 现在它抛出了一个错误,但是显示数据库查询以及“sql中的图像不能为空”。我希望它像常规表单验证一样显示,而不显示sql查询和错误 它似乎仍然在提交表单时没有首先抛出错误,因为它说,$post\u image未定义。 我已经尝试使用Php 图像上载仍在提交,并在CI中进行回调验证,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在尝试在codeigniter 3上进行图像上传验证。我尝试了很多图坦卡蒙,但仍然面临这个问题。我想,如果用户将上传照片区域留空(最终,如果他们上传不支持的mime和大小),则在允许或尝试提交数据之前抛出错误 现在它抛出了一个错误,但是显示数据库查询以及“sql中的图像不能为空”。我希望它像常规表单验证一样显示,而不显示sql查询和错误 它似乎仍然在提交表单时没有首先抛出错误,因为它说,$post\u image未定义。 我已经尝试使用isset函数输入回调函数,尝试仅在上载图像时允许表单提
isset
函数输入回调函数,尝试仅在上载图像时允许表单提交数据,否则,我尝试通过传递错误变量来显示错误。正如我上面所解释的,所有这些似乎都不能正常工作
控制器:
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
视图:
照片
在上传图像之前,您可以使用jquery在客户端检查mime和大小。在上传图像之前,您可以使用jquery在客户端检查mime和大小。
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>