Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vm's avatar
Level 2

How to let user play audio and video files stored in directory above public

I have audio and video files stored in a directory above the public directory -- how will I render a view in blade with this content? I don't want to put the files in public since they are user confidential -- any suggestions ? Thanks P.S. I have not yet converted to laravel 5 -- on the list to do

0 likes
4 replies
thepsion5's avatar

You will serve the files from a route. For example, if you wanted someone to have access to a PDF stored in your storage directory, you'd use this route:

Route::get('/pdfs/something', [
    'as' => 'pdf',
    'uses' => 'PdfController@viewPdf'
];

And in your controller:

function viewPdf($id)
{
    $filename = storage_path() . "/pdfs/$id.pdf";
    $headers = array(
        'Content-type'          => 'application/pdf',
        'Content-Disposition'   => 'inline; filename="' . $filename . '"'
    );
    return Response::make( file_get_contents($filename), 200, $headers);
}

Now you can do this in your view:

<a href="{{ route('pdf', 1) }}">open PDF</a>

It would work the same way with just about any other type of file.

2 likes
vm's avatar
Level 2

@thepsion5 thanks for the quick reply. When I do this it downloads the file and opens itunes (on my mac) and plays the mp4a file. What I am looking for is a way in which user can play the file using a javascript plugin audio/video player included in the view, without downloading the file. Is that possible. Also what is the empty quotes at the end of content disposition?

bobbybouwmann's avatar

For the video part try something like this: (http://www.w3schools.com/html/html5_video.asp)

<video width="400" controls>
    <source src="{{ route('pdf', 1) }}" type="video/mp4">
    Your browser does not support HTML5 video.
</video>

About the code part, if you don't have the extra quotes your array will look like this:

// $filename = index.php
array [
    'Content-type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="index.php'
];

While you want it to look like this:

// $filename = index.php
array [
    'Content-type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="index.php"' // Notice the double quotes at the end!
];
1 like
jaydipsinh's avatar

Route::get('video/{user_id}/{name}', function ($user_id,$name) { $base = asset('uploads/users/' . $user_id.'/'.$name); $headers = array( 'Content-type' => 'video/mp4', 'Content-Disposition' => 'inline; filename="' . $name . '"' );

   base64_encode(file_get_contents($base)));
return  Response::make( file_get_contents($base), 200, $headers);

});

Please or to participate in this conversation.