先来看看前端代码。我不选择文件,直接提交
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="icon">
<button type="submit">提交</button>
</form>后端使用Request::has()判断返回真halt(Request::has('icon', 'file', true)); // bool(true)在has()方法内断点 public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool
{
if (!in_array($type, ['param', 'get', 'post', 'put', 'patch', 'route', 'delete', 'cookie', 'session', 'env', 'request', 'server', 'header', 'file'])) {
return false;
}
$param = empty($this->$type) ? $this->$type() : $this->$type;
// 断点
halt($param);
if (is_object($param)) {
return $param->has($name);
}
// 按.拆分成多维数组进行判断
foreach (explode('.', $name) as $val) {
if (isset($param[$val])) {
$param = $param[$val];
} else {
return false;
}
}
return ($checkEmpty && '' === $param) ? false : true;
}返回array(1) {
["icon"] => array(5) {
["name"] => string(0) ""
["type"] => string(0) ""
["tmp_name"] => string(0) ""
["error"] => int(4) // UPLOAD_ERR_NO_FILE 没有文件被上传
["size"] => int(0)
}
}所以对于file的has()判断是否有必要需要改进?然后,再换一种方法
$file = Request::file('icon'); // 这里直接报错了!错误:没有文件被上传!
if (null !== $file) {
echo '没上传文件';
}所以对于判断文件有没有被上传,应该怎么做/cry最佳答案