【问题标题】:How to upload and Validate Image with Form Data in CodeIgniter?如何在 CodeIgniter 中使用表单数据上传和验证图像?
【发布时间】:2018-11-24 03:41:20
【问题描述】:

问题是如何同时验证图像和表单数据

$this->load->library('form_validation');
$this->form_validation->set_message('file_check', 'jpg attachement allowed Only');
$this->form_validation->set_rules('file', '', 'callback_file_check');
$this->form_validation->set_rules('number','Number','trim|required|min_length[10]|max_length[10]');

if($this->form_validation->run()){
    $name=$this->input->post('name');
    $subject=$this->input->post('subject');
    $messege=$this->input->post('messege');
    $number=$this->input->post('number');

    $configs['upload_path']   = './uploads/';
    $configs['allowed_types'] = 'jpg';
    $this->load->library('upload', $configs);

    //upload file to directory
    if($this->upload->do_upload('file') ){
        $upload_data = $this->upload->data();
        $this->load->library('email',$config);
        $this->email->attach($upload_data['full_path']);
        $this->email->set_newline("\r\n");
        $email_body ="<div >".$messege."</div>";
        $this->email->set_mailtype("html");
        $this->email->from('mail@gmail.com',$name);
        $this->email->to('mailto@gmail.com');
        $this->email->subject($subject);
        $this->email->message($email_body);
        $this->email->send();
        $this->index();
    }
}    

【问题讨论】:

    标签: php codeigniter validation file-upload


    【解决方案1】:

    您可以使用自定义验证功能来做到这一点。

    function do_upload()
    {
      $this->load->library('form_validation'); 
      $this->form_validation->set_rules('file', 'File', 'callback_file_check'); 
      $this->form_validation->set_rules('number','Number','trim|required|min_length[10]|max_length[10]');
      if($this->form_validation->run()==TRUE)
      {
        //upload image
      }
      else
      {
        // return form error
      }
    }
    
    function file_check($str){
      // load file helper 
      $this->load->helper('file');
      // image mime type
      $allowed_mime_type_arr = array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
      $mime = get_mime_by_extension($_FILES['file']['name']);
      // file is uploaded and not empty
      if(isset($_FILES['file']['name']) && $_FILES['file']['name']!="")
      {
        if(in_array($mime, $allowed_mime_type_arr))
        {
          return TRUE;
        }
        else
        {
          // return false with custome error message
          $this->form_validation->set_message('file_check', 'Please select only gif/jpg/png file.');
          return FALSE;
        }
      }
      else
      {
    
        $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
        return FALSE;
      }
    }
    

    【讨论】:

    • get_mime_by_extension($_FILES['file']['name']) 这个函数没有定义??
    • 确保在函数 get_mime_by_extension($_FILES['file']['name']) 之前加载文件助手
    猜你喜欢
    • 2011-07-07
    • 2016-04-13
    • 1970-01-01
    • 2018-04-10
    • 2015-11-16
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多