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

johnDoe220's avatar

How to read file from private disk in laravel

hi,i created private disk in laravel storage,my question is how to read or access to this file?

0 likes
37 replies
johnDoe220's avatar

@theProfit The documentation does not mention how you can access the file from a drive you created. Note that I can download the file when I try to download it,but can not To show

Sinnbeck's avatar

What do you need to do with it? Download? Get content?

return Storage::disk('diskname')->download('file.jpg');
//or
return Storage::disk('diskname')->get('file.jpg'); 
//or show
$pathToFile = Storage::disk('diskname')->path('file.jpg');
return response()->file($pathToFile); 
johnDoe220's avatar

@Sinnbeck The documentation does not mention how you can access the file from a drive you created. Note that I can download the file when I try to download it,but can not To show the get method doese't to show

johnDoe220's avatar

@Sinnbeck i try read video file for show into edit form with id,but doesen't to show video,for example if file uploaded to public directory i can show file with this code

storage/.$media->video
Sinnbeck's avatar

@johnDoe220 start by making sure that it works if you move the file to the public folder and call it directly in the browser

johnDoe220's avatar

@Sinnbeck yes it's work but i will this files uploaded into private directory,because i not will files show from url in private folder

johnDoe220's avatar

@Sinnbeck this codes doesen't to work

Storage::disk('private')->get($media->video)
asset('storage/'.$media->video)
Storage::disk('private')->path($media->video)

but download work

Storage::disk('private')->download($video->video);
johnDoe220's avatar

Of course, I must add that the file can be read with this code

Storage::disk('private')->get($media->video)

But my problem is that it can not be displayed in the video tag, for example

<video src="{{ None of the above methods worked in this tag }}"></video>
johnDoe220's avatar

@Sinnbeck The file is downloaded with this code

$pathToFile = Storage::disk('private')->path($media->video);
        return response()->file($pathToFile);
Sinnbeck's avatar

@johnDoe220 I just tested it out and it works as expected.

  1. Downloaded a test mp4 file and put it in storage/app
  2. Add an endpoint to return the file (see first code snippet)
  3. Add a video tag on a page, that loads the first endpoint (see second code snippet)
Route::get('video', function () {
    return return Storage::disk('local')->response('video.mp4'); //changed this to a single line, as that works as well. You can use the path/response version instead if you like.
});

and in blade

 <h1>Video should be shown below</h1>
<video src="{{ url('video')  }}" controls></video>

https://i.imgur.com/f8s76xK.png

johnDoe220's avatar

@Sinnbeck See what I did, write this code on videosController into show method for example

$show = Storage::disk('local')->response('test.mp4');

and passing $show data to show.blade.php page,and result this

Unable to retrieve the mime_type for file at location: test.mp4. 

and when write this code on web.php

Route::get('video', function () {
    return return Storage::disk('local')->response('test.mp4');
});

and write video url on video tag,this resualt give me

<video src="http://localhost:8000/video"></video>
Sinnbeck's avatar

@johnDoe220 Where did you find that file? Can you test with the first file on that page I linked to?

johnDoe220's avatar

@Sinnbeck For testing, I have now moved my video file to the articles directory in the public directory, and the file was easily loaded for me via the following code.

<video src="{{ asset('storage/articles/test.mp4') }}"></video>
Snapey's avatar

what part of "private" did you not understand?

johnDoe220's avatar

@Snapey To upload my own video files instead of using Laravel's default directory to upload (public) I created a directory called Private and uploaded my files to it correctly and I can even easily download them from this directory But my problem is that I can not display the file in the form of a video tag in any way

Snapey's avatar

@johnDoe220 you cannot give someone a link to a private file

what you can do is create a route and a controller method that goes and gets the file from the private storage and then sends it to the user.

It's slower than letting them access the file directly and it's memory heavy since php must load the file first

The fact that you can't understand why download works but putting a link on an http response does not just illustrates you don't really understand the concepts

Why are you storing it privately?

Sinnbeck's avatar

@Snapey Be aware that it does work just fine to return the file from php :) I tested it above and had no problems. But I completely agree that it can be an issue loading a 300 mb video file into memory

1 like
johnDoe220's avatar

@Snapey Just so that the user does not have access to the file via URL, although the file is first hashed and then stored in memory, I have done this before in a way that can not be accessed from the URL, but it worked very well.

johnDoe220's avatar

@Sinnbeck My files will eventually be 50 MB in size, so far I have not really had any problems with the projects I created loading or overuse of resources, the only difference between this project and other projects is that I want direct access to the files Do not have

Sinnbeck's avatar

@Snapey Sorry must have misunderstood :) I thought thats what you meant by "you cannot give someone a link to a private file"

Please or to participate in this conversation.