douzhaochan6468 2019-02-25 08:35
浏览 53
已采纳

使用单输入上传多个图像时,Codeigniter验证不起作用

I am trying to upload multiple images in codeigniter using single input its working fine but i also want to add codeigniter validation on this input field but it not works. Here's my html code,

<input type="file" name="images[]" id="file" multiple="">

And Here's my codeigniter Code,

if (empty($_FILES['images']['name']))
{
    $this->form_validation->set_rules('images', 'Item Image', 'required');
}

Can anyone please tell me why this validation not working when i try to change images name to images[] in codeigniter then this will always says image is required rather the image is selected.

  • 写回答

4条回答 默认 最新

  • duanrenchuo9244 2019-02-25 13:28
    关注

    Create the validation function yourself using callback:

    public function index()
    {
        $this->load->helper('file');
        $this->load->library('form_validation');
        if($this->input->post('submit'))
        {
            $this->form_validation->set_rules('file', '', 'callback_file_check');
            if($this->form_validation->run() == TRUE)
            {
                // Upload
            }
        }
        $this->load->view('upload');
    }
    
    public function file_check($str)
    {
        $allowed_mime_type_arr = array('image/gif','image/jpeg','image/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 gif/jpg/png file.');
                return FALSE;
            }
        }
        else
        {
            $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
            return FALSE;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题