gpt4 book ai didi

php - Laravel:多个图像上的图像验证不起作用

转载 作者:行者123 更新时间:2023-12-02 20:25:48 25 4
gpt4 key购买 nike

注册用户可以创建帖子,并且通过该帖子,用户可以上传多张图片。但是我遇到的问题是它无法正确验证文件上传。我用过:

mimes:jpg,jpeg,png

但是,当我上传这些类型的图像时,它说它们不是。在实现多个图像上传之前,我上传了 1 个图像,这很好,但是当我上传多个图像时,它就破坏了。

这是我用于创建帖子的 postController.php 方法:

public function postCreatePost(Request $request){
//validates each field
$this->validate($request, [
'title' => 'required|max:100',
'type' => 'required',
'subtype' => 'required|max:100',
'date' => 'required|date',
'venue' => 'required|max:100',
'body' => 'required',
'cover_image' => 'required|mimes:jpeg,jpg,png'
]);

//adds each field sent in the request to the relevant post table column
$post = new Post();
$post->title = $request['title'];
$post->type = $request['type'];
$post->subtype = $request['subtype'];
$post->date = $request['date'];
$post->venue = $request['venue'];
$post->body = $request['body'];



$message = 'There was an error';
if($request->user()->posts($post)->save($post)){ //submits record to post table and if succesful it will enter loop

//creats image name and stores it in images table and stores the image in the cover_images directory
if($request->hasFile('cover_image')){
foreach($request->file('cover_image') as $file){
$filenameWithExt = $file->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $file->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$path = $file->storeAs('public/cover_images', $fileNameToStore);
$image = new Image();
$image->cover_image = $fileNameToStore;
$image->post_id = $post->id;
$image->save();
}
}

$message = 'post successfully created';
}

return redirect()->route('dashboard')->with(['message' => $message]); // redirects user back to the dashboard
}

最佳答案

试试这个...

'cover_image.*' => 'required|mimes:jpeg,jpg,png'

或者这个...

'cover_image.*.file' => 'required|mimes:jpeg,jpg,png'

关于php - Laravel:多个图像上的图像验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50008783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com