Request类增加paramWithFile方法 包含上传文件信息

Validate类增加image验证规则 并改进max min length支持多种数据类型
This commit is contained in:
thinkphp
2016-07-18 14:35:24 +08:00
parent 0b96346f2e
commit 215225bc5f
2 changed files with 85 additions and 46 deletions

View File

@@ -626,6 +626,22 @@ class Request
return $this->input($this->param, $name, $default, $filter); return $this->input($this->param, $name, $default, $filter);
} }
/**
* 获取获取当前请求的参数 包含file
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function paramWithFile($name = '', $default = null, $filter = null)
{
$param = $this->param(false);
$file = $this->file();
$data = array_merge($param, $file);
return $this->input($data, $name, $default, $filter);
}
/** /**
* 设置获取获取路由参数 * 设置获取获取路由参数
* @access public * @access public
@@ -934,6 +950,9 @@ class Request
return $default; return $default;
} }
} }
if (is_object($data)) {
return $data;
}
} }
// 解析过滤器 // 解析过滤器
@@ -986,7 +1005,7 @@ class Request
if (is_callable($filter)) { if (is_callable($filter)) {
// 调用函数或者方法过滤 // 调用函数或者方法过滤
$value = call_user_func($filter, $value); $value = call_user_func($filter, $value);
} else { } elseif (is_scalar($value)) {
if (strpos($filter, '/')) { if (strpos($filter, '/')) {
// 正则过滤 // 正则过滤
if (!preg_match($filter, $value)) { if (!preg_match($filter, $value)) {

View File

@@ -43,6 +43,7 @@ class Validate
'accepted' => ':attribute必须是yes、on或者1', 'accepted' => ':attribute必须是yes、on或者1',
'date' => ':attribute格式不符合', 'date' => ':attribute格式不符合',
'file' => ':attribute不是有效的上传文件', 'file' => ':attribute不是有效的上传文件',
'image' => ':attribute不是有效的图像文件',
'alpha' => ':attribute只能是字母', 'alpha' => ':attribute只能是字母',
'alphaNum' => ':attribute只能是字母和数字', 'alphaNum' => ':attribute只能是字母和数字',
'alphaDash' => ':attribute只能是字母、数字和下划线_及破折号-', 'alphaDash' => ':attribute只能是字母、数字和下划线_及破折号-',
@@ -79,6 +80,7 @@ class Validate
'fileSize' => '上传文件大小不符', 'fileSize' => '上传文件大小不符',
'fileExt' => '上传文件后缀不符', 'fileExt' => '上传文件后缀不符',
'fileMime' => '上传文件类型不符', 'fileMime' => '上传文件类型不符',
]; ];
// 当前验证场景 // 当前验证场景
@@ -385,33 +387,6 @@ class Validate
return true !== $result ? $result : true; return true !== $result ? $result : true;
} }
/**
* 验证表单令牌(需要配置令牌生成行为)
* @access protected
* @param mixed $value 字段值
* @param mixed $rule 验证规则
* @param array $data 数据
* @return bool
*/
protected function token($value, $rule, $data)
{
if (!isset($data[$rule]) || !isset($_SESSION[$rule])) {
// 令牌数据无效
return false;
}
// 令牌验证
list($key, $value) = explode('_', $data[$rule]);
if (isset($_SESSION[$rule][$key]) && $value && $_SESSION[$rule][$key] === $value) {
// 防止重复提交
unset($_SESSION[$rule][$key]); // 验证完成销毁session
return true;
}
// 开启TOKEN重置
unset($_SESSION[$rule][$key]);
return false;
}
/** /**
* 验证是否和某个字段的值一致 * 验证是否和某个字段的值一致
* @access protected * @access protected
@@ -569,8 +544,10 @@ class Validate
$result = is_array($value); $result = is_array($value);
break; break;
case 'file': case 'file':
$file = Request::instance()->file($value); $result = $value instanceof \think\File;
$result = !empty($file); break;
case 'image':
$result = $value instanceof \think\File && exif_imagetype($value->getRealPath());
break; break;
default: default:
if (isset(self::$type[$rule])) { if (isset(self::$type[$rule])) {
@@ -614,14 +591,13 @@ class Validate
/** /**
* 验证上传文件后缀 * 验证上传文件后缀
* @access protected * @access protected
* @param mixed $value 字段值 * @param mixed $file 上传文件
* @param mixed $rule 验证规则 * @param mixed $rule 验证规则
* @return bool * @return bool
*/ */
protected function fileExt($value, $rule) protected function fileExt($file, $rule)
{ {
$file = Request::instance()->file($value); if (!($file instanceof \think\File)) {
if (empty($file)) {
return false; return false;
} }
if (is_string($rule)) { if (is_string($rule)) {
@@ -642,14 +618,13 @@ class Validate
/** /**
* 验证上传文件类型 * 验证上传文件类型
* @access protected * @access protected
* @param mixed $value 字段值 * @param mixed $file 上传文件
* @param mixed $rule 验证规则 * @param mixed $rule 验证规则
* @return bool * @return bool
*/ */
protected function fileMime($value, $rule) protected function fileMime($file, $rule)
{ {
$file = Request::instance()->file($value); if (!($file instanceof \think\File)) {
if (empty($file)) {
return false; return false;
} }
if (is_string($rule)) { if (is_string($rule)) {
@@ -670,14 +645,13 @@ class Validate
/** /**
* 验证上传文件大小 * 验证上传文件大小
* @access protected * @access protected
* @param mixed $value 字段值 * @param mixed $file 上传文件
* @param mixed $rule 验证规则 * @param mixed $rule 验证规则
* @return bool * @return bool
*/ */
protected function fileSize($value, $rule) protected function fileSize($file, $rule)
{ {
$file = Request::instance()->file($value); if (!($file instanceof \think\File)) {
if (empty($file)) {
return false; return false;
} }
if (is_string($rule)) { if (is_string($rule)) {
@@ -685,7 +659,7 @@ class Validate
} }
if (is_array($file)) { if (is_array($file)) {
foreach ($file as $item) { foreach ($file as $item) {
if (!$item->checkExt($rule)) { if (!$item->checkSize($rule)) {
return false; return false;
} }
} }
@@ -695,6 +669,33 @@ class Validate
} }
} }
/**
* 验证图片的宽高及类型
* @access protected
* @param mixed $file 上传文件
* @param mixed $rule 验证规则
* @return bool
*/
protected function image($file, $rule)
{
if (!($file instanceof \think\File)) {
return false;
}
$rule = explode(',', $rule);
list($width, $height, $type) = getimagesize($file->getRealPath());
if (isset($rule[2])) {
$imageType = strtolower($rule[2]);
if ('jpeg' == $imageType) {
$imageType = 'jpg';
}
if (image_type_to_extension($type) != $imageType) {
return false;
}
}
list($w, $h) = $rule;
return $w == $width && $h == $height;
}
/** /**
* 验证请求类型 * 验证请求类型
* @access protected * @access protected
@@ -914,7 +915,14 @@ class Validate
*/ */
protected function length($value, $rule) protected function length($value, $rule)
{ {
$length = strlen((string) $value); // 当前数据长度 if (is_array($value)) {
$length = count($value);
} elseif ($value instanceof \think\File) {
$length = $value->getSize();
} else {
$length = mb_strlen((string) $value);
}
if (strpos($rule, ',')) { if (strpos($rule, ',')) {
// 长度区间 // 长度区间
list($min, $max) = explode(',', $rule); list($min, $max) = explode(',', $rule);
@@ -934,7 +942,13 @@ class Validate
*/ */
protected function max($value, $rule) protected function max($value, $rule)
{ {
$length = strlen((string) $value); if (is_array($value)) {
$length = count($value);
} elseif ($value instanceof \think\File) {
$length = $value->getSize();
} else {
$length = mb_strlen((string) $value);
}
return $length <= $rule; return $length <= $rule;
} }
@@ -947,7 +961,13 @@ class Validate
*/ */
protected function min($value, $rule) protected function min($value, $rule)
{ {
$length = strlen((string) $value); if (is_array($value)) {
$length = count($value);
} elseif ($value instanceof \think\File) {
$length = $value->getSize();
} else {
$length = mb_strlen((string) $value);
}
return $length >= $rule; return $length >= $rule;
} }