Hi @johndoe220 this is the second question on the same topic, please read https://laravel.com/docs/9.x/filesystem
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?
@theProfit doesn't write
@johnDoe220 what do you mean ?
@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
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);
@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 added a show example. What is the file type?
@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
@johnDoe220 start by making sure that it works if you move the file to the public folder and call it directly in the browser
@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 can you show what you currently doing that isn't working?
@Sinnbeck now no access to system,I will send it to you in another hour
@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);
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 try my last example on a link that is added to that src
@Sinnbeck The file is downloaded with this code
$pathToFile = Storage::disk('private')->path($media->video);
return response()->file($pathToFile);
@johnDoe220 I just tested it out and it works as expected.
- Downloaded a test mp4 file and put it in
storage/app - Add an endpoint to return the file (see first code snippet)
- 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>
@Sinnbeck The result was unexpected
<video src="http://localhost:8000/video"></video>
@johnDoe220 I dont know what that means. Is that an error on the page? In my test I downloaded the first file from here to test with: https://file-examples.com/index.php/sample-video-files/sample-mp4-files/
And it works if I just call it in the browser directly http:;//localhost/video
@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>
@johnDoe220 Where did you find that file? Can you test with the first file on that page I linked to?
@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>
@johnDoe220 Yes. But can you test with my example code here https://laracasts.com/discuss/channels/laravel/how-to-read-file-from-private-disk-in-laravel?reply=776534, with the first video example here: https://file-examples.com/index.php/sample-video-files/sample-mp4-files/
It works without any problems for me, in laravel 8 as shown in the linked image
what part of "private" did you not understand?
@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
@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?
@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
@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.
@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 Do you have time to use AnyDesk to see my source code and check the codes?
@Sinnbeck i didn't say it wasn't possible to serve the file from php?
@Snapey Sorry must have misunderstood :) I thought thats what you meant by "you cannot give someone a link to a private file"
Old documentation mentioned how to access files https://laravel.com/docs/5.4/filesystem
Old documentation mentioned how to access files https://laravel.com/docs/5.4/filesystem#retrieving-files
anyone reading this article check this out it helped me: www.stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5/
Please or to participate in this conversation.