El Klo
El Klo

Reputation: 322

Retrieve video from Laravel Storage

Currently I am sending a video to the storage folder in Laravel through the following piece of code inside a create() function:

if ($request->hasFile('video_url')) {
    $video = $request->file('video_url');
    $folder = make_url($validated['title']);
    $video_name = time().'-vid.'. $video->getClientOriginalExtension();

    Storage::disk('local')->put('/uploads/' . $folder . '/', $video);

    $validated['video_url'] = $folder . '/' . $video->hashName();
}

It creates the video, send it to the proper folder and it sends the URL of the video to the database so that i can always retrieve the correct link. This all works fine.

I have no idea how to retrieve the video for playback from storage though.

I have tried to retrieve the link manually inside the video tags but, of course, the storage folder is not public so this did not work.

{{$vid = Storage::disk('local')->get('/uploads/' . $e_course_chapter->video_url)}}

{{ var_dump($vid) }}

<video controls>

    <source src="{{  Storage::disk('local')->get('/uploads/' . $e_course_chapter->video_url) }}" type="video/mp4">
    Your browser does not support the video tag.

</video>

The var_dump gives me this:

/home/vagrant/code/storage/framework/views/777c4fe762b993d5be04ad6550ee11200bd4869d.php:49:string '���ftypmp42����mp42isomavc1����free�������������������������������������������������������������������������������������������������������������������������������mdat�� ���E���H��,� �#��x264 - core 79 - H.264/MPEG-4 AVC codec - Copyleft 2003-2009 - http://www.videolan.org/x264.html - options: cabac=0 ref=2 deblock=1:0:0 analyse=0x1:0x111 me=umh subme=6 psy=1 psy_rd=1.0:0.0 mixed_ref=1 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 chroma_qp_offset=-2 threads=6 nr=0 decimate=1 mbaff=0'... (length=383631)

Upvotes: 3

Views: 8463

Answers (2)

Arvind Kala
Arvind Kala

Reputation: 570

You can create a route from which you can get the video and use that route in view.

In Controller:

function getVideo() {
    $video = Storage::disk('local')->get("uploadpath_here");
    $response = Response::make($video, 200);
    $response->header('Content-Type', 'video/mp4');
    return $response;
}

In View:

use <source src="{{ route('/get-video') }} ... >

In Route:

Route::get('/get-video', Controller@getVideo);

which in turn will call the controller function.

Upvotes: 6

Jasper Helmich
Jasper Helmich

Reputation: 751

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

To create the symbolic link, you may use the storage:link Artisan command:

php artisan storage:link

Upvotes: 1

Related Questions