File类错误信息支持多语言

This commit is contained in:
thinkphp
2017-10-18 12:04:34 +08:00
parent 03d4502043
commit a177f1ac46
2 changed files with 105 additions and 73 deletions

View File

@@ -66,4 +66,20 @@ return [
'relation data not exists' => '关联数据不存在', 'relation data not exists' => '关联数据不存在',
'relation not support' => '关联不支持', 'relation not support' => '关联不支持',
'chunk not support order' => 'Chunk不支持调用order方法', 'chunk not support order' => 'Chunk不支持调用order方法',
// 上传错误信息
'unknown upload error' => '未知上传错误!',
'file write error' => '文件写入失败!',
'upload temp dir not found' => '找不到临时文件夹!',
'no file to uploaded' => '没有文件被上传!',
'only the portion of file is uploaded' => '文件只有部分被上传!',
'upload File size exceeds the maximum value' => '上传文件大小超过了最大值!',
'upload write error' => '文件上传保存错误!',
'has the same filename: {:filename}' => '存在同名文件:{:filename}',
'upload illegal files' => '非法上传文件',
'illegal image files' => '非法图片文件',
'extensions to upload is not allowed' => '上传文件后缀不允许',
'mimetype to upload is not allowed' => '上传文件MIME类型不允许',
'filesize not match' => '上传文件大小不符!',
'directory {:path} creation failed' => '目录 {:path} 创建失败!',
]; ];

View File

@@ -96,7 +96,7 @@ class File extends SplFileObject
/** /**
* 获取文件的哈希散列值 * 获取文件的哈希散列值
* @param string $type * @param string $type
* @return mixed $string * @return string
*/ */
public function hash($type = 'sha1') public function hash($type = 'sha1')
{ {
@@ -120,7 +120,7 @@ class File extends SplFileObject
if (mkdir($path, 0755, true)) { if (mkdir($path, 0755, true)) {
return true; return true;
} else { } else {
$this->error = "目录 {$path} 创建失败!"; $this->error = ['directory {:path} creation failed', ['path' => $path]];
return false; return false;
} }
} }
@@ -180,28 +180,27 @@ class File extends SplFileObject
/* 检查文件大小 */ /* 检查文件大小 */
if (isset($rule['size']) && !$this->checkSize($rule['size'])) { if (isset($rule['size']) && !$this->checkSize($rule['size'])) {
$this->error = '上传文件大小不符!'; $this->error = 'filesize not match';
return false; return false;
} }
/* 检查文件Mime类型 */ /* 检查文件Mime类型 */
if (isset($rule['type']) && !$this->checkMime($rule['type'])) { if (isset($rule['type']) && !$this->checkMime($rule['type'])) {
$this->error = '上传文件MIME类型不允许'; $this->error = 'mimetype to upload is not allowed';
return false; return false;
} }
/* 检查文件后缀 */ /* 检查文件后缀 */
if (isset($rule['ext']) && !$this->checkExt($rule['ext'])) { if (isset($rule['ext']) && !$this->checkExt($rule['ext'])) {
$this->error = '上传文件后缀不允许'; $this->error = 'extensions to upload is not allowed';
return false; return false;
} }
/* 检查图像文件 */ /* 检查图像文件 */
if (!$this->checkImg()) { if (!$this->checkImg()) {
$this->error = '非法图像文件!'; $this->error = 'illegal image files';
return false; return false;
} }
return true; return true;
} }
@@ -215,7 +214,9 @@ class File extends SplFileObject
if (is_string($ext)) { if (is_string($ext)) {
$ext = explode(',', $ext); $ext = explode(',', $ext);
} }
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION)); $extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
if (!in_array($extension, $ext)) { if (!in_array($extension, $ext)) {
return false; return false;
} }
@@ -229,6 +230,7 @@ class File extends SplFileObject
public function checkImg() public function checkImg()
{ {
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION)); $extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
/* 对图像文件进行严格检测 */ /* 对图像文件进行严格检测 */
if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6, 13])) { if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6, 13])) {
return false; return false;
@@ -274,9 +276,11 @@ class File extends SplFileObject
if (is_string($mime)) { if (is_string($mime)) {
$mime = explode(',', $mime); $mime = explode(',', $mime);
} }
if (!in_array(strtolower($this->getMime()), $mime)) { if (!in_array(strtolower($this->getMime()), $mime)) {
return false; return false;
} }
return true; return true;
} }
@@ -297,7 +301,7 @@ class File extends SplFileObject
// 检测合法性 // 检测合法性
if (!$this->isValid()) { if (!$this->isValid()) {
$this->error = '非法上传文件'; $this->error = 'upload illegal files';
return false; return false;
} }
@@ -305,6 +309,7 @@ class File extends SplFileObject
if (!$this->check()) { if (!$this->check()) {
return false; return false;
} }
$path = rtrim($path, DS) . DS; $path = rtrim($path, DS) . DS;
// 文件保存命名规则 // 文件保存命名规则
$saveName = $this->buildSaveName($savename); $saveName = $this->buildSaveName($savename);
@@ -317,7 +322,7 @@ class File extends SplFileObject
/* 不覆盖同名文件 */ /* 不覆盖同名文件 */
if (!$replace && is_file($filename)) { if (!$replace && is_file($filename)) {
$this->error = '存在同名文件' . $filename; $this->error = ['has the same filename: {:filename}', ['filename' => $filename]];
return false; return false;
} }
@@ -325,13 +330,15 @@ class File extends SplFileObject
if ($this->isTest) { if ($this->isTest) {
rename($this->filename, $filename); rename($this->filename, $filename);
} elseif (!move_uploaded_file($this->filename, $filename)) { } elseif (!move_uploaded_file($this->filename, $filename)) {
$this->error = '文件上传保存错误!'; $this->error = 'upload write error';
return false; return false;
} }
// 返回 File对象实例 // 返回 File对象实例
$file = new self($filename); $file = new self($filename);
$file->setSaveName($saveName); $file->setSaveName($saveName);
$file->setUploadInfo($this->info); $file->setUploadInfo($this->info);
return $file; return $file;
} }
@@ -362,12 +369,14 @@ class File extends SplFileObject
} }
} }
} }
} elseif ('' === $savename) { } elseif ('' === $savename || false === $savename) {
$savename = $this->getInfo('name'); $savename = $this->getInfo('name');
} }
if (!strpos($savename, '.')) { if (!strpos($savename, '.')) {
$savename .= '.' . pathinfo($this->getInfo('name'), PATHINFO_EXTENSION); $savename .= '.' . pathinfo($this->getInfo('name'), PATHINFO_EXTENSION);
} }
return $savename; return $savename;
} }
@@ -380,32 +389,39 @@ class File extends SplFileObject
switch ($errorNo) { switch ($errorNo) {
case 1: case 1:
case 2: case 2:
$this->error = '上传文件大小超过了最大值!'; $this->error = 'upload File size exceeds the maximum value';
break; break;
case 3: case 3:
$this->error = '文件只有部分被上传!'; $this->error = 'only the portion of file is uploaded';
break; break;
case 4: case 4:
$this->error = '没有文件被上传!'; $this->error = 'no file to uploaded';
break; break;
case 6: case 6:
$this->error = '找不到临时文件夹!'; $this->error = 'upload temp dir not found';
break; break;
case 7: case 7:
$this->error = '文件写入失败!'; $this->error = 'file write error';
break; break;
default: default:
$this->error = '未知上传错误!'; $this->error = 'unknown upload error';
} }
} }
/** /**
* 获取错误信息 * 获取错误信息(支持多语言)
* @return mixed * @return string
*/ */
public function getError() public function getError()
{ {
return $this->error; if (is_array($this->error)) {
list($msg, $vars) = $this->error;
} else {
$msg = $this->error;
$vars = [];
}
return Lang::has($msg) ? Lang::get($msg, $vars) : $msg;
} }
public function __call($method, $args) public function __call($method, $args)