Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter-带有图像上载的表单验证_Php_Codeigniter_File Upload - Fatal编程技术网

Php CodeIgniter-带有图像上载的表单验证

Php CodeIgniter-带有图像上载的表单验证,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我有一个表单,它接受一些文本输入字段和一个文件字段(用于上传图像) 我的问题是,如果我没有填写一个必填字段,并且选择了一个有效的图像文件,我将收到一条关于缺少字段的错误消息,但是图像将被上传。控制器是: defined('BASEPATH') OR exit('No direct script access allowed'); class Manage extends MY_Controller { public function __construc

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

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

    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;
            }
        }
    }
正如我所知,如果一个规则失败,其他字段和操作将被取消,表单将再次加载,或者其他操作将被完成


谢谢,,,

仅当验证为真时,您需要上传图像。因此,删除您的公共函数upload_image(),并在下面给出的validation true语句中编写Functionists

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

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

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

好的,你现在有什么问题?你想要什么?您正在获取的错误如果我没有填写其中一个必填字段,并且选择了有效的图像文件,我将收到一条关于缺少字段的错误消息,但图像仍将上载。如果“必填字段”是指具有HTML 5必填属性的表单字段,则表单将不会提交。因此,图像将不会上载。已使用CodeIgniter中的form_验证规则而不是HTML属性设置了所需的条件。因此,如果验证为false,图像将不会上载?