我正在尝试上传多个文件,其名称在不同的字段中。 例如---有3个表单字段用于插入名称,3个字段用于按相应顺序上载文件。 这是附加的图像,我想要实现的目标 -
现在我想要的是,当用户在第一个字段的“文档名称”标签中输入名称并选择要在“文档文件”标签第一个字段中上传的文件时,则插入数据。否则显示错误。 用户应在各自的顺序中输入文件名选择文件,否则应显示错误。 当用户在第一个字段中输入名称并在第二个字段中选择一个文件然后它应该显示错误时应该出错。 这是在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”。
document.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));
}
答案 0 :(得分:0)
将文档名称字段放在文档文件字段旁边,并首先隐藏它。然后在选择文件时显示文档名称字段(jQuery - Detecting if a file has been selected in the file input)。
在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