简体   繁体   English

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

[英]Codeigniter Validation Not Working When Uploading Multiple Images Using Single Input

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. 我正在尝试使用单个输入在codeigniter中上载多个图像,但其效果还不错,但我也想在此输入字段上添加codeigniter验证,但它不起作用。 Here's my html code, 这是我的html代码,

<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. 谁能告诉我为什么当我尝试在codeigniter中将图像名称更改为images []时,此验证不起作用,那么这总是表示需要图像而不是选择了图像。

Try to apply validation on the count of $_FILES['images']['name'] . 尝试对$_FILES['images']['name']的计数应用验证。

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

Try this code 试试这个代码

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

Your input name is name="images[]" So try this one... 您的输入名称为name =“ images []”,因此请尝试此操作...

$this->form_validation->set_rules('images[]', 'Item Image', 'required');

So you can call form validation when you have no files on POST. 因此,当您在POST上没有文件时,可以调用表单验证。

if(count($_FILES['images']['name']) < 1) {
    $this->form_validation->set_rules('images[]', 'Item Image', 'required');
}else {
   // upload files
}

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM