【问题标题】:CodeIgniter - form validation with image uploadCodeIgniter - 带有图像上传的表单验证
【发布时间】:2016-04-13 23:18:02
【问题描述】:

我有一个接受一些文本输入字段和一个文件字段(用于上传图像)的表单。

我的问题是这样的,如果我没有填写一个必填字段并且我选择了一个有效的图像文件,我会收到关于缺少字段的错误消息,但图像会被上传。控制器是:

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Manage extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            $config = array(
                array(
                    'field' => 'item_name',
                    'label' => 'Item name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'item_code',
                    'label' => 'Item code',
                    'rules' => 'required|is_unique[_items.item_code]'
                ),
                array(
                    'field' => 'item_description',
                    'label' => 'Item description',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'item_img',
                    'label' => 'Item image',
                    'rules' => 'callback_upload_image'
                )
            );

            $this->form_validation->set_rules($config);
            $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');

            if($this->form_validation->run() == FALSE)
            {
                // Render the entry form again
            }
            else
            {
                // Go to another page
            }
        }

        public function upload_image()
        {
            $config['upload_path'] = './items_images';
            $config['max_size'] = 1024 * 10;
            $config['allowed_types'] = 'gif|png|jpg|jpeg';
            $config['encrypt_name'] = TRUE;

            $this->load->library('upload', $config);

            if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
            {
                if($this->upload->do_upload('item_img'))
                {
                    $upload_data = $this->upload->data();
                    $_POST['item_img'] = $upload_data['file_name'];
                    return TRUE;
                }
                else
                {
                    $this->form_validation->set_message('upload_image', $this->upload->display_errors());
                    return FALSE;
                }
            }
            else
            {
                $_POST['item_img'] = NULL;
                return FALSE;
            }
        }
    }

据我所知,如果一个规则失败,其他字段和操作将被取消,并重新加载表单,或者将执行其他操作。

谢谢你,,,

【问题讨论】:

  • 好的,你现在有什么问题?你想要什么?你得到什么错误
  • 如果我没有填写一个必填字段并且我选择了一个有效的图像文件,我会收到关于缺少字段的错误消息,但图像仍会上传。
  • 如果必填字段是指具有 HTML 5 必填属性的表单字段,则表单将不会提交。所以图片不会被上传。
  • 所需条件已使用 CodeIgniter 中的 form_validation 规则设置,而不是来自 HTML 属性。
  • 如果验证为假,图片不应该上传?

标签: php codeigniter file-upload


【解决方案1】:

只有在验证为真时才需要上传图片。所以删除你的公共函数 upload_image() 并在验证 true 语句中编写功能主义者,如下所示。

 defined('BASEPATH') OR exit('No direct script access allowed');
class Manage extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $config = array(
            array(
                'field' => 'item_name',
                'label' => 'Item name',
                'rules' => 'required'
            ),
            array(
                'field' => 'item_code',
                'label' => 'Item code',
                'rules' => 'required|is_unique[_items.item_code]'
            ),
            array(
                'field' => 'item_description',
                'label' => 'Item description',
                'rules' => 'required'
            ),
            array(
                'field' => 'item_img',
                'label' => 'Item image',
                'rules' => 'callback_upload_image'
            )
        );

        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');

        if($this->form_validation->run() == FALSE)
        {
            // Render the entry form again
        }
        else
        {
           $config['upload_path'] = './items_images';
        $config['max_size'] = 1024 * 10;
        $config['allowed_types'] = 'gif|png|jpg|jpeg';
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);

        if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
        {
            if($this->upload->do_upload('item_img'))
            {
                $upload_data = $this->upload->data();
                $_POST['item_img'] = $upload_data['file_name'];
                return TRUE;
            }
            else
            {
                $this->form_validation->set_message('upload_image', $this->upload->display_errors());
                return FALSE;
            }
        }
        else
        {
            $_POST['item_img'] = NULL;
            return FALSE;
        }
    }
  // Go to another page
        }
    }

【讨论】:

    【解决方案2】:

    我发现这篇很有帮助的帖子here。作者编写了自己的规则来验证用户选择上传的文件。代码示例:

    $this->form_validation->set_rules('file', '', 'callback_file_check');
    
    
    public function file_check($str){
        $allowed_mime_type_arr = array('application/pdf','image/gif','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('file_check', 'Please select only pdf/gif/jpg/png file.');
                return false;
            }
        }else{
            $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多