How to upload video files in laravel?

by giovanny.lueilwitz , in category: PHP Frameworks , 6 months ago

How to upload video files in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 6 months ago

@giovanny.lueilwitz 

To upload video files in Laravel, you can follow these steps:

  1. Create a form in your view file where users can upload their video files. Make sure to set the form enctype attribute to "multipart/form-data" in order to upload files.
1
2
3
4
5
<form action="{{ route('upload.video') }}" method="post" enctype="multipart/form-data">
    @csrf
    <input type="file" name="video">
    <button type="submit">Upload Video</button>
</form>


  1. Create a route to handle the file upload request in your routes/web.php file.
1
Route::post('/upload-video', 'VideoController@upload')->name('upload.video');


  1. In your VideoController.php file, create a method to handle the file upload and store the video file in the storage directory.
1
2
3
4
5
6
7
public function upload(Request $request)
{
    $video = $request->file('video');
    $videoPath = $video->store('videos', 'public');

    return "Video uploaded successfully!";
}


  1. Make sure to run the storage:link Artisan command to create a symbolic link from public/storage to storage/app/public.
1
php artisan storage:link


  1. You can now access the uploaded video file using the storage URL in your view files.
1
2
3
4
<video controls>
    <source src="{{ asset('storage/' . $videoPath) }}" type="video/mp4">
    Your browser does not support the video tag.
</video>


That's it! You have now successfully uploaded a video file in Laravel.