英文:
TypeError Illegal offset type vaildator for images mimes types
问题
以下是您要的翻译内容:
"The following code will verify whether an image is a JPG, JPEG, or PNG."
以下代码将验证图像是否为JPG、JPEG或PNG。
"I tried to upload a jpeg file, but the validator somehow throws an error."
我尝试上传一个jpeg文件,但验证器似乎出现了错误。
"how the request appears It receives 4 photos and is supposed to verify whether they are images before uploading them to the server."
请求的外观如何,它接收4张照片并应该在将它们上传到服务器之前验证它们是否为图像。
["+files: Symfony\Component\HttpFoundation\FileBag {#55 ▼"
["+文件:Symfony\Component\HttpFoundation\FileBag {#55 ▼"
"#parameters: array:4 [▼"
"#参数:数组:4 [▼"
" "image1" => Symfony\Component\HttpFoundation\File\UploadedFile {#37 ▼"
" "image1" => Symfony\Component\HttpFoundation\File\UploadedFile {#37 ▼"
" -test: false"
" -test:false"
" -originalName: "2.jpeg""
" -originalName:"2.jpeg""
" -mimeType: "image/jpeg""
" -mimeType:"image/jpeg""
" -error: 0"
" -error:0"
" path: "/private/var/tmp""
" 路径:"/private/var/tmp""
" filename: "php4m2tRN""
" 文件名:"php4m2tRN""
" basename: "php4m2tRN""
" 基本名称:"php4m2tRN""
" pathname: "/private/var/tmp/php4m2tRN""
" 路径名:"/private/var/tmp/php4m2tRN""
" extension: """
" 扩展名:"""
" realPath: "/private/var/tmp/php4m2tRN""
" 真实路径:"/private/var/tmp/php4m2tRN""
" aTime: 2023-02-23 18:48:09"
" aTime:2023-02-23 18:48:09"
" mTime: 2023-02-23 18:48:09"
" mTime:2023-02-23 18:48:09"
" cTime: 2023-02-23 18:48:09"
" cTime:2023-02-23 18:48:09"
" inode: 1342583"
" inode:1342583"
" size: 45014"
" 大小:45014"
" perms: 0100600"
" 权限:0100600"
" owner: 501"
" 所有者:501"
" group: 0"
" 组:0"
" type: "file""
" 类型:"file""
" writable: true"
" 可写:true"
" readable: true"
" 可读:true"
" executable: false"
" 可执行:false"
" file: true"
" 文件:true"
" dir: false"
" 目录:false"
" link: false"
" 链接:false"
" }"
" }"
英文:
The following code will verify whether an image is a JPG, JPEG, or PNG.
I tried to upload a jpeg file, but the validator somehow throws an error.
$validate = Validator::make($request->all(), [
$request->file('image1') => 'required|mimes:jpg,png,jpeg',
$request->file('image2') => 'required|mimes:jpg,png,jpeg',
$request->file('image3') => 'required|mimes:jpg,png,jpeg',
$request->file('image4') => 'required|mimes:jpg,png,jpeg',
]);
// dd($validate[0]);
if( $validate->fails() ){
return response($validate->errors(), 400);
}
how the request appears It receives 4 photos and is supposed to verify whether they are images before uploading them to the server.
+files: Symfony\Component\HttpFoundation\FileBag {#55 ▼
#parameters: array:4 [▼
"image1" => Symfony\Component\HttpFoundation\File\UploadedFile {#37 ▼
-test: false
-originalName: "2.jpeg"
-mimeType: "image/jpeg"
-error: 0
path: "/private/var/tmp"
filename: "php4m2tRN"
basename: "php4m2tRN"
pathname: "/private/var/tmp/php4m2tRN"
extension: ""
realPath: "/private/var/tmp/php4m2tRN"
aTime: 2023-02-23 18:48:09
mTime: 2023-02-23 18:48:09
cTime: 2023-02-23 18:48:09
inode: 1342583
size: 45014
perms: 0100600
owner: 501
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
"image2" => Symfony\Component\HttpFoundation\File\UploadedFile {#38 ▶}
"image3" => Symfony\Component\HttpFoundation\File\UploadedFile {#39 ▶}
"image4" => Symfony\Component\HttpFoundation\File\UploadedFile {#40 ▶}
]
}
答案1
得分: 1
创建验证器的正确方法是将文件名作为键添加,具体文档可以在 https://laravel.com/docs/10.x/validation#manually-creating-validators 中找到。
$validate = Validator::make($request->all(), [
'image1' => 'required|mimes:jpg,png,jpeg',
'image2' => 'required|mimes:jpg,png,jpeg',
'image3' => 'required|mimes:jpg,png,jpeg',
'image4' => 'required|mimes:jpg,png,jpeg',
]);
英文:
The correct way to create the Validator would be to add the file names as keys https://laravel.com/docs/10.x/validation#manually-creating-validators
$validate = Validator::make($request->all(), [
'image1' => 'required|mimes:jpg,png,jpeg',
'image2' => 'required|mimes:jpg,png,jpeg',
'image3' => 'required|mimes:jpg,png,jpeg',
'image4' => 'required|mimes:jpg,png,jpeg',
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论