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

@@ -12,58 +12,74 @@
// 核心中文语言包 // 核心中文语言包
return [ return [
// 系统错误提示 // 系统错误提示
'Undefined variable' => '未定义变量', 'Undefined variable' => '未定义变量',
'Undefined index' => '未定义数组索引', 'Undefined index' => '未定义数组索引',
'Undefined offset' => '未定义数组下标', 'Undefined offset' => '未定义数组下标',
'Parse error' => '语法解析错误', 'Parse error' => '语法解析错误',
'Type error' => '类型错误', 'Type error' => '类型错误',
'Fatal error' => '致命错误', 'Fatal error' => '致命错误',
'syntax error' => '语法错误', 'syntax error' => '语法错误',
// 框架核心错误提示 // 框架核心错误提示
'dispatch type not support' => '不支持的调度类型', 'dispatch type not support' => '不支持的调度类型',
'method param miss' => '方法参数错误', 'method param miss' => '方法参数错误',
'method not exists' => '方法不存在', 'method not exists' => '方法不存在',
'module not exists' => '模块不存在', 'module not exists' => '模块不存在',
'controller not exists' => '控制器不存在', 'controller not exists' => '控制器不存在',
'class not exists' => '类不存在', 'class not exists' => '类不存在',
'property not exists' => '类的属性不存在', 'property not exists' => '类的属性不存在',
'template not exists' => '模板文件不存在', 'template not exists' => '模板文件不存在',
'illegal controller name' => '非法的控制器名称', 'illegal controller name' => '非法的控制器名称',
'illegal action name' => '非法的操作名称', 'illegal action name' => '非法的操作名称',
'url suffix deny' => '禁止的URL后缀访问', 'url suffix deny' => '禁止的URL后缀访问',
'Route Not Found' => '当前访问路由未定义', 'Route Not Found' => '当前访问路由未定义',
'Undefined db type' => '未定义数据库类型', 'Undefined db type' => '未定义数据库类型',
'variable type error' => '变量类型错误', 'variable type error' => '变量类型错误',
'PSR-4 error' => 'PSR-4 规范错误', 'PSR-4 error' => 'PSR-4 规范错误',
'not support total' => '简洁模式下不能获取数据总数', 'not support total' => '简洁模式下不能获取数据总数',
'not support last' => '简洁模式下不能获取最后一页', 'not support last' => '简洁模式下不能获取最后一页',
'error session handler' => '错误的SESSION处理器类', 'error session handler' => '错误的SESSION处理器类',
'not allow php tag' => '模板不允许使用PHP语法', 'not allow php tag' => '模板不允许使用PHP语法',
'not support' => '不支持', 'not support' => '不支持',
'redisd master' => 'Redisd 主服务器错误', 'redisd master' => 'Redisd 主服务器错误',
'redisd slave' => 'Redisd 从服务器错误', 'redisd slave' => 'Redisd 从服务器错误',
'must run at sae' => '必须在SAE运行', 'must run at sae' => '必须在SAE运行',
'memcache init error' => '未开通Memcache服务请在SAE管理平台初始化Memcache服务', 'memcache init error' => '未开通Memcache服务请在SAE管理平台初始化Memcache服务',
'KVDB init error' => '没有初始化KVDB请在SAE管理平台初始化KVDB服务', 'KVDB init error' => '没有初始化KVDB请在SAE管理平台初始化KVDB服务',
'fields not exists' => '数据表字段不存在', 'fields not exists' => '数据表字段不存在',
'where express error' => '查询表达式错误', 'where express error' => '查询表达式错误',
'no data to update' => '没有任何数据需要更新', 'no data to update' => '没有任何数据需要更新',
'miss data to insert' => '缺少需要写入的数据', 'miss data to insert' => '缺少需要写入的数据',
'miss complex primary data' => '缺少复合主键数据', 'miss complex primary data' => '缺少复合主键数据',
'miss update condition' => '缺少更新条件', 'miss update condition' => '缺少更新条件',
'model data Not Found' => '模型数据不存在', 'model data Not Found' => '模型数据不存在',
'table data not Found' => '表数据不存在', 'table data not Found' => '表数据不存在',
'delete without condition' => '没有条件不会执行删除操作', 'delete without condition' => '没有条件不会执行删除操作',
'miss relation data' => '缺少关联表数据', 'miss relation data' => '缺少关联表数据',
'tag attr must' => '模板标签属性必须', 'tag attr must' => '模板标签属性必须',
'tag error' => '模板标签错误', 'tag error' => '模板标签错误',
'cache write error' => '缓存写入失败', 'cache write error' => '缓存写入失败',
'sae mc write error' => 'SAE mc 写入错误', 'sae mc write error' => 'SAE mc 写入错误',
'route name not exists' => '路由标识不存在(或参数不够)', 'route name not exists' => '路由标识不存在(或参数不够)',
'invalid request' => '非法请求', 'invalid request' => '非法请求',
'bind attr has exists' => '模型的属性已经存在', 'bind attr has exists' => '模型的属性已经存在',
'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;
} }
@@ -285,7 +289,7 @@ class File extends SplFileObject
* @param string $path 保存路径 * @param string $path 保存路径
* @param string|bool $savename 保存的文件名 默认自动生成 * @param string|bool $savename 保存的文件名 默认自动生成
* @param boolean $replace 同名文件是否覆盖 * @param boolean $replace 同名文件是否覆盖
* @return false|File false-失败 否则返回File实例 * @return false|File false-失败 否则返回File实例
*/ */
public function move($path, $savename = true, $replace = true) public function move($path, $savename = true, $replace = 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)