mime类型验证在Laravel中的工作原理

时间:2017-03-16 14:30:13

标签: laravel laravel-5.2 laravel-validation

我已经尝试过不同的方法来验证文件,但它仍然无效。我尝试过的验证规则是ff:mimes& MIME类型。我想验证要上传的文件是单个文件还是多个文件。这是我要验证文件的代码

 $this->validate($request, [
        'body' => 'required|max:120',
        'file' => 'mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    $user = Auth::user();

    $post = new Post();
    $post->body = $request['body'];
    $post->user_id = $user->id;
    $files = $request['file'];
    $post->save();

    if ($request->hasFile('file')) :
        foreach ($files as $file):
            $filename = time() . '.' . $file->getClientOriginalExtension();
            $originalname = $file->getClientOriginalName();
            $location = public_path('attachments/');
            $file->move($location, $file->getClientOriginalName());
            $post_file = new File();
            $post_file->file_name = $filename;
            $post_file->previous_file_name = $originalname;
            $post_file->user_id = $user->id;
            $post->files()->save($post_file);
            $post_file->save();
        endforeach;
    endif;

1 个答案:

答案 0 :(得分:0)

将此行替换为您的。

  'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'