修复本地上传未限制文件格式的BUG

pull/29/head
Karson 2017-12-23 12:23:56 +08:00
parent 3a92ef5564
commit 5a87939e86
4 changed files with 43 additions and 11 deletions

View File

@ -49,7 +49,7 @@ class Ajax extends Backend
$file = $this->request->file('file'); $file = $this->request->file('file');
if (empty($file)) if (empty($file))
{ {
$this->error("未上传文件或超出服务器上传限制"); $this->error(__('No file upload or server upload limit exceeded'));
} }
//判断是否已经存在附件 //判断是否已经存在附件
@ -64,6 +64,14 @@ class Ajax extends Backend
$fileInfo = $file->getInfo(); $fileInfo = $file->getInfo();
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
$suffix = $suffix ? $suffix : 'file'; $suffix = $suffix ? $suffix : 'file';
$mimetypeArr = explode(',', $upload['mimetype']);
$typeArr = explode('/', $fileInfo['type']);
//验证文件后缀
if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr) && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))
{
$this->error(__('Uploaded file format is limited'));
}
$replaceArr = [ $replaceArr = [
'{year}' => date("Y"), '{year}' => date("Y"),
'{mon}' => date("m"), '{mon}' => date("m"),
@ -110,7 +118,7 @@ class Ajax extends Backend
$attachment->data(array_filter($params)); $attachment->data(array_filter($params));
$attachment->save(); $attachment->save();
\think\Hook::listen("upload_after", $attachment); \think\Hook::listen("upload_after", $attachment);
$this->success('上传成功', null, [ $this->success(__('Upload successful'), null, [
'url' => $uploadDir . $splInfo->getSaveName() 'url' => $uploadDir . $splInfo->getSaveName()
]); ]);
} }

View File

@ -0,0 +1,7 @@
<?php
return [
'No file upload or server upload limit exceeded' => '未上传文件或超出服务器上传限制',
'Uploaded file format is limited' => '上传文件格式受限制',
'Upload successful' => '上传成功',
];

View File

@ -37,17 +37,15 @@ class Ajax extends Frontend
*/ */
public function upload() public function upload()
{ {
Config::set('default_return_type', 'json');
$file = $this->request->file('file'); $file = $this->request->file('file');
if (empty($file))
{
$this->error(__('No file upload or server upload limit exceeded'));
}
//判断是否已经存在附件 //判断是否已经存在附件
$sha1 = $file->hash(); $sha1 = $file->hash();
$uploaded = model("attachment")->where('sha1', $sha1)->find();
if ($uploaded)
{
$this->success('', null, [
'url' => $uploaded['url']
]);
}
$upload = Config::get('upload'); $upload = Config::get('upload');
@ -58,6 +56,14 @@ class Ajax extends Frontend
$fileInfo = $file->getInfo(); $fileInfo = $file->getInfo();
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
$suffix = $suffix ? $suffix : 'file'; $suffix = $suffix ? $suffix : 'file';
$mimetypeArr = explode(',', $upload['mimetype']);
$typeArr = explode('/', $fileInfo['type']);
//验证文件后缀
if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr) && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))
{
$this->error(__('Uploaded file format is limited'));
}
$replaceArr = [ $replaceArr = [
'{year}' => date("Y"), '{year}' => date("Y"),
'{mon}' => date("m"), '{mon}' => date("m"),
@ -97,10 +103,14 @@ class Ajax extends Frontend
'mimetype' => $fileInfo['type'], 'mimetype' => $fileInfo['type'],
'url' => $uploadDir . $splInfo->getSaveName(), 'url' => $uploadDir . $splInfo->getSaveName(),
'uploadtime' => time(), 'uploadtime' => time(),
'storage' => 'local',
'sha1' => $sha1, 'sha1' => $sha1,
); );
model("attachment")->create(array_filter($params)); $attachment = model("attachment");
$this->success('', null, [ $attachment->data(array_filter($params));
$attachment->save();
\think\Hook::listen("upload_after", $attachment);
$this->success(__('Upload successful'), null, [
'url' => $uploadDir . $splInfo->getSaveName() 'url' => $uploadDir . $splInfo->getSaveName()
]); ]);
} }

View File

@ -0,0 +1,7 @@
<?php
return [
'No file upload or server upload limit exceeded' => '未上传文件或超出服务器上传限制',
'Uploaded file format is limited' => '上传文件格式受限制',
'Upload successful' => '上传成功',
];