douxian1923 2017-01-01 08:32
浏览 36
已采纳

是否有更好的多部分/表单数据验证方法

I have a multipart/form-data with an image upload and some personal data, so I want to include file upload in form validation, I can successfully do this.

However, I now find that there is an issue, ie even if my other form fields have errors and upload file field with no error, then image uploads to folder, how to prevent this, I mean, in my case, If name, email, file fields validation is ok then only file should upload, if name filed validation fails and file field validation ok then file should not upload

here is the code I use:

In Controller:

 <?php
    public $_rules = array(
            'name'=>array('field'=>'name', 'label'=>'Name', 'rules'=>'trim|required'),
            'email'=>array('field'=>'email', 'label'=>'Email', 'rules'=>'trim|required|valid_email'),       
            'profile_img'=>array('field'=>'profile_img', 'label'=>'Design', 'rules'=>'callback__profile_upload')
            );

public function profile()
    {       
        $this->load->library('upload');
        $rules = $this->_rules; 
        $this->form_validation->set_rules($rules);

        if($this->form_validation->run()==TRUE){
            die('success');
        }else {         
            $this->data['content'] =  'frontend/pages/place_order';
            $this->load->view('frontend/_layout_main', $this->data);
        }

    }

function _profile_upload(){
  if($_FILES['profile_img']['size'] != 0 && !empty($_FILES['profile_img']) ){
    $upload_dir = './profile_pics/';
    if (!is_dir($upload_dir)) {
         mkdir($upload_dir);
    }   
    $config['upload_path']   = $upload_dir;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['file_name']     = 'profile_img_'.substr(md5(rand()),0,7);
    $config['overwrite']     = false;
    $config['max_size']  = '5120';

     $this->upload->initialize($config);
        if (!$this->upload->do_upload('profile_img')){
            $this->form_validation->set_message('_profile_upload', $this->upload->display_errors());
            return false;
        }   
        else{
            $this->upload_data['file'] =  $this->upload->data();
            return true;
        }   
    }   
    else{
        $this->form_validation->set_message('_profile_upload', "No file selected");
        return false;
    }
}

IN VIEW:

<?php echo form_open_multipart();?>
 <?php $name_err = (!empty(form_error('name'))) ? 'err' : ' ';  
     echo form_input('name',set_value('name'), array('placeholder'=>'Name','class'=>" {$name_err } "));
     ?>

 <?php $email_err = (!empty(form_error('email'))) ? 'err' : ' ';  
    echo form_input('email',set_value('email'), array('placeholder'=>'EMail','class'=>" {$email_err } "));
     ?> 
<?php    
echo form_error('profile_img');
echo form_upload(array('name' =>'profile_img', 'class' => 'inputfile inputfile-4', 'id' => 'profile_img'));
?>
     <li><input type="submit" class="special" value="Submit" /></li>
  • 写回答

3条回答 默认 最新

  • drws65968272 2017-01-10 05:31
    关注

    I have got a solution from codexWorld, I have asked same question over there, and they replied with a tutorial, If anybody still looking for a solution here is the link

    http://www.codexworld.com/codeigniter-file-upload-validation/

    in the validation call back, we just want to do the file type check

    public function _profile_upload($str){
            $allowed_mime_type_arr = array('image/jpeg','image/pjpeg','image/png','image/x-png');
            $mime = get_mime_by_extension($_FILES['file']['name']);
            if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){
                if(in_array($mime, $allowed_mime_type_arr)){
                    return true;
                }else{
                    $this->form_validation->set_message('_profile_upload', 'Please select only pdf/gif/jpg/png file.');
                    return false;
                }
            }else{
                $this->form_validation->set_message('_profile_upload', 'Please choose a file to upload.');
                return false;
            }
        }
    

    and in the main function we just want to do the same as normal, using do_upload and specifying the upload config items, anyhow there will be a second file type check when the function executes, I think that doesn't matter. The code will look like this::

    public function profile()
        {       
            $rules = $this->_rules; 
            $this->form_validation->set_rules($rules);
    
            if($this->form_validation->run()==TRUE){
                 $config['upload_path']   = 'uploads/files/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']      = 1024;
                $this->load->library('upload', $config);
                //upload file to directory
                if($this->upload->do_upload('file')){
                    $uploadData = $this->upload->data();
                    $uploadedFile = $uploadData['file_name'];
    
                    /*
                     *insert file information into the database
                     *.......
                     */
    
                 }else{
                    $data['error_msg'] = $this->upload->display_errors();
                }
            }else {         
                $this->data['content'] =  'frontend/pages/place_order';
                $this->load->view('frontend/_layout_main', $this->data);
            }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制