@nickywan123 You will need to use multi-part uploading to upload a video file given they tend to be large files. If you try and upload this in a “normal” HTTP request, then you will either hit timeout or memory limit errors.
Apr 15, 2021
4
Level 6
How to upload video?
I want to allow user to choose to update a normal post, an image or video. So far, I can upload post and image and they both hit the same controller.
thread controller:
public function store(Request $request){
//create thread for post
if($request->has('post')){
//validate the request
$this->validate($request,[
'title' => 'required',
'body' => 'required',
'channel_id' => 'required|exists:channels,id'
]);
//Create a new instance of thread
$thread = Thread::create([
'user_id' => auth()->id(),
'channel_id' => request('channel_id'),
'title' => request('title'),
'body' => request('body')
]);
// Success new thread created
Session::flash('message', 'Thread has been published');
return redirect($thread->path());
}
//create thread for image/video
if($request->has('image')){
//check if request has image
if($request->hasFile('image')){
//check if image is valid uploaded
if($request->file('image')->isValid()){
$validated = $request->validate([
'title' => 'required',
'channel_id' => 'required|exists:channels,id',
'image' => 'required|mimes:jpeg,png,jpg|max:1014',
]);
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(500,500)->save(public_path('/images/uploads/threads/images/'. $filename));
//create new post
$thread = Thread::create([
'user_id' => auth()->id(),
'channel_id' => request('channel_id'),
'title' => request('title'),
'image' => $filename
]);
// Success new thread created
Session::flash('message', 'Thread has been published');
return redirect($thread->path());
}
return back()->with('error-image','Image is not valid');
}
}
return back()->with('error-image','Please upload an image');
}
However, how do I go about the validation rules to validate what video files and size it can fit?
Or should I create a separate controller just for uploading video?
What's the best way to handle this?
Please or to participate in this conversation.