如何在php中验证多文件上传字段


how to validate multiple file upload field in php?

我正在尝试上传几个文件,它们的名称在不同的字段中。例如,有3个表单字段用于插入名称,3个字段用于上传文件。这是我想要实现的图像

https://i.stack.imgur.com/QaZFn.jpg

现在我想要的是,当用户在第一个字段的"文档名称"标签中输入名称,并在"文档文件"标签第一个字段中选择要上传的文件时,数据就会被插入。否则显示错误。用户应输入文件名,按相应顺序选择文件,否则应显示错误。当用户在第一个字段中输入名称并在第二个字段中选择文件时,它应该会出错,然后它应该显示错误。这是在opencart管理部分完成的。

目前,这是用于简单验证的简单代码——

foreach ($this->request->post['document_description'] as $language_id => $value) {
        if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 64)) {
            $this->error['name'][$language_id] = $this->language->get('error_name');
        }
    }

文件名字段和表单上传字段之间的一个共同点是"语言id"。

documents.php的代码是---

protected function getForm() {
    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['entry_name'] = $this->language->get('entry_name');
    $this->data['entry_filename'] = $this->language->get('entry_filename');
    $this->data['entry_mask'] = $this->language->get('entry_mask');
    $this->data['entry_remaining'] = $this->language->get('entry_remaining');
    $this->data['entry_update'] = $this->language->get('entry_update');
    $this->data['button_save'] = $this->language->get('button_save');
    $this->data['button_cancel'] = $this->language->get('button_cancel');
    $this->data['button_upload'] = $this->language->get('button_upload');
    if (isset($this->error['warning'])) {
        $this->data['error_warning'] = $this->error['warning'];
    } else {
        $this->data['error_warning'] = '';
    }
    if (isset($this->error['name'])) {
        $this->data['error_name'] = $this->error['name'];
    } else {
        $this->data['error_name'] = array();
    }
    if (isset($this->error['filename'])) {
        $this->data['error_filename'] = $this->error['filename'];
    } else {
        $this->data['error_filename'] = '';
    }

    $this->data['breadcrumbs'] = array();
    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('text_home'),
        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
        'separator' => false
    );
    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('heading_title'),
        'href'      => $this->url->link('catalog/documents', 'token=' . $this->session->data['token'] . $url, 'SSL'),           
        'separator' => ' :: '
    );
    if (!isset($this->request->get['document_id'])) {
        $this->data['action'] = $this->url->link('catalog/documents/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
    } else {
        $this->data['action'] = $this->url->link('catalog/documents/update', 'token=' . $this->session->data['token'] . '&document_id=' . $this->request->get['document_id'] . $url, 'SSL');
    }
    $this->data['cancel'] = $this->url->link('catalog/documents', 'token=' . $this->session->data['token'] . $url, 'SSL');
    $this->load->model('localisation/language');
    $this->data['languages'] = $this->model_localisation_language->getLanguages();
    if (isset($this->request->get['document_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
        $document_info = $this->model_catalog_documents->getDocument($this->request->get['document_id']);
    }
    $this->data['token'] = $this->session->data['token'];
    if (isset($this->request->get['document_id'])) {
        $this->data['document_id'] = $this->request->get['document_id'];
    } else {
        $this->data['document_id'] = 0;
    }
    if (isset($this->request->post['document_description'])) {
        $this->data['document_description'] = $this->request->post['document_description'];
    } elseif (isset($this->request->get['document_id'])) {
        $this->data['document_description'] = $this->model_catalog_documents->getDocumentDescriptions($this->request->get['document_id']);
    } else {
        $this->data['document_description'] = array();
    }   
    if (isset($this->request->post['filename'])) {
        $this->data['filename'] = $this->request->post['filename'];
    } elseif (!empty($document_info)) {
        $this->data['filename'] = $document_info['filename'];
    } else {
        $this->data['filename'] = '';
    }
    if (isset($this->request->post['mask'])) {
        $this->data['mask'] = $this->request->post['mask'];
    } elseif (!empty($document_info)) {
        $this->data['mask'] = $document_info['mask'];       
    } else {
        $this->data['mask'] = '';
    }
    if (isset($this->request->post['remaining'])) {
        $this->data['remaining'] = $this->request->post['remaining'];
    } elseif (!empty($document_info)) {
        $this->data['remaining'] = $document_info['remaining'];
    } else {
        $this->data['remaining'] = 1;
    }
    if (isset($this->request->post['update'])) {
        $this->data['update'] = $this->request->post['update'];
    } else {
        $this->data['update'] = false;
    }
    $this->template = 'catalog/documents_form.tpl';
    $this->children = array(
        'common/header',
        'common/footer'
    );
    $this->response->setOutput($this->render());    
}

protected function validateForm() { 
        if (!$this->user->hasPermission('modify', 'catalog/documents')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        print_r($this->request->post['document_description']); echo "<br>";
        print_r($this->request->files['document_files']); echo "<br>";
        foreach ($this->request->files['document_files'] as $files => $value)
        {
            print_r($files.'==>'.print_r($value)); echo "<br>"; 
        }die;
        foreach ($this->request->post['document_description'] as $language_id => $value) {
            foreach ($this->request->files['document_files'] as $selected => $filename)
            {
                if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 64)) 
                {   
                    $this->error['name'][$language_id] = $this->language->get('error_name');
                }
                if (empty($filename))
                {
                    $this->error['name'][$language_id] = $this->language->get('error_name');
                }
            }
        }   
        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

public function upload() {
        $this->language->load('sale/order');
        $json = array();
        if (!$this->user->hasPermission('modify', 'catalog/documents')) {
            $json['error'] = $this->language->get('error_permission');
        }   
        if (!isset($json['error'])) {   
            if (!empty($this->request->files['file']['name'])) {
                $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
                if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
                    $json['error'] = $this->language->get('error_filename');
                }       
                // Allowed file extension types
                $allowed = array();
                $filetypes = explode("'n", $this->config->get('config_file_extension_allowed'));
                foreach ($filetypes as $filetype) {
                    $allowed[] = trim($filetype);
                }
                if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
                    $json['error'] = $this->language->get('error_filetype');
                }   
                // Allowed file mime types      
                $allowed = array();
                $filetypes = explode("'n", $this->config->get('config_file_mime_allowed'));
                foreach ($filetypes as $filetype) {
                    $allowed[] = trim($filetype);
                }
                if (!in_array($this->request->files['file']['type'], $allowed)) {
                    $json['error'] = $this->language->get('error_filetype');
                }
                if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
                    $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
                }
                if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
                    $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
                }
            } else {
                $json['error'] = $this->language->get('error_upload');
            }
        }
        if (!isset($json['error'])) {
            if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
                $ext = md5(mt_rand());
                $json['filename'] = $filename . '.' . $ext;
                $json['mask'] = $filename;
                move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $filename . '.' . $ext);
            }
            $json['success'] = $this->language->get('text_upload');
        }   
        $this->response->setOutput(json_encode($json));
    }

将文档名称字段放在文档文件字段旁边,并首先将其隐藏。然后在选择文件时显示文档名称字段(jQuery-检测是否在文件输入中选择了文件)。

admin/controller/catalog/product.php中,有一个函数:validateForm,所有验证都在其中完成。在该功能中,foreach用于基于语言的验证:

foreach ($this->request->post['product_description'] as $language_id => $value) {
...........
//add your validation code here like:
    if (trim($value['download_name']) == '' && $value['download_file'] == '' ) {
        $this->error['downloadfile'][$language_id] = $this->language->get('error_ownloadfile');
    }
...........
}// end of foreach