0
我已經嘗試了不同的方式來驗證文件,但它仍然無法正常工作。我嘗試的驗證規則是ff:mimes & mimetypes。我想驗證要上傳的文件是單個文件還是多個文件。下面是我想驗證文件MIME類型驗證如何在Laravel中工作
$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;
仍然返回相同的錯誤。如果有文件大小限制,則另一個錯誤是 方法[validate2048]不存在。 validation.file 該文件必須是類型爲jpeg,png,jpg,gif,svg的文件。 – Mark
@kenneth親切地檢查一下,我已經編輯了這一行。並讓我知道然後 –
仍然無法正常工作。 – Mark