User57
User57

Reputation: 2505

Laravel How to make download link only for authenticate user?

I have some files on public directory files folder. I want to send the user the link of a specific file.Right now I'm giving the link by following way.

<a href="{{asset('files/'.$row->file_path)}}">
    <button class="btn btn-success">Download</button>
</a>

By which actually user know where my directory is..Can i make the download link that way in which only authenticate user can download the file or i can hide the directory from url?

Upvotes: 1

Views: 1517

Answers (1)

kopaty4
kopaty4

Reputation: 2296

First, move all the files from the publicly accessible directory to private, for example, to storage_path('private/') folder.

Secondly, create a method that will receive in any way a file name from the URL (decoding it from query, or load from database by id), make authorization / some other things checks, and if it's all right, give it to the client for download. This can be done as follows: return response()->download($pathToFile);

Then, instead of using asset() with a direct link to the file, give users a link to the route that executes the created method.

Upvotes: 2

Related Questions