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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了