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

LadyDeathKZN's avatar

Can't download File

Hi, I am battling to download a file in my storage directory

Directory is: storage/app/upload/client/

after the client/ the stored client_id - > user->name is used and then the file name. I currently have this as the download button, but it isn't rendering anything.

<a href="{{storage_path()}}/upload/client/{{$teamform->forms->users->name}}/{{$teamform->forms->upload}}" class="btn btn-outline-primary float-right" target="_blank"><i class="fa fa-download"></i> Download </a>
0 likes
14 replies
schaeferalex's avatar

You need to put the file path in the helper function, like:

{{ storage_path('/upload/client/' . $teamform->forms->users->name . '/' . $teamform->forms->upload) }}
LadyDeathKZN's avatar

Thank you, just tried your solution and it isn't doing anything. Mmmm... I also tried adding app/ and still nothing. When using the devtools it says file is not found, I have checked and the link is indeed correct and corresponds to the file directory.

LadyDeathKZN's avatar

When hovering over the link it says file:// and then the link, could this be the problem?

click's avatar

You can't download files from your storage directory. Unless they are in the storage/app/public directory as described in the docs: https://laravel.com/docs/5.8/filesystem#the-public-disk

Did you store the file there?

And storage_path() returns an absolute path of your server. Not an http:// path or a relative path to your public directory. So that method is not useful when you ant to link to a file.

On a side note, are you sure you want to have these files publicly accessible aren't the files upload private to the user?

EslamAhmed's avatar

Hi,

I suggest you create a new route for downloading those files from the storage and use this method from laravel documentation https://laravel.com/docs/5.8/filesystem#downloading-files

or it could be something like that

return response()->download(storage_path("app/upload/client/....."));

Make sure to secure this route if these files are restricted to a specific user role.

_______________

Another way if you want to make those files accessible from public you can follow the documentation by using

php artisan storage:link

here https://laravel.com/docs/5.8/filesystem#configuration

But I really prefer the first approach

LadyDeathKZN's avatar

Hi thank you for that, I have done php artisan storage:link. The files are uploaded by one user with a role but needs to be accessible to another user with another role.

example user with role team user with role client.

These files are not to be public but they need to be downloaded. The client uploads a file, the team download it and reupload the edited file which the client should beable to download.

Would the route support this?

I am currently using storage/app/upload/user_role (team or client)/user name/ then the file:

{{ storage_path('upload/client/' . $teamform->forms->users->name . '/' . $teamform->forms->upload) }}
schaeferalex's avatar

You can't put that link into an html tag as the file is not publicly accessible. You need to create a route, handle the request in your controller and then send a response to the client: https://laravel.com/docs/5.8/responses#file-downloads

Example:

return response()->download(storage_path('/app/upload/client/test.txt'));
LadyDeathKZN's avatar

Hey again, I have read the documentation and a few tutorials. I am just not getting the hang of this. I need to pass 2 variables to the route - $teamform->forms->users->name and $teamform->forms->upload.

web.php

//Team Download
Route::get('team-form/{file}', 'TeamFormController@downloadClientFile')->name('clientDownload');

controller

public function downloadClientFile() {
        return response()->download(storage_path('/app/upload/client/'));
    }

I looked at this tutorial and understand the concept: https://stackoverflow.com/questions/20415444/download-files-in-laravel-using-responsedownload

But I just don't see how it will work with 2 variables, I am still very new to Laravel and some of the methods are hard to recreate with what I would like.

LadyDeathKZN's avatar
<a href="{{ route('clientDownload') . '/' . $teamform->forms->users->name . '/' . $teamform->forms->upload }}" class="btn btn-outline-primary float-right" target="_blank"><i class="fa fa-download"></i> Download </a>
EslamAhmed's avatar
Level 2

You can do something like that:

web.php

Route::get('team-form/{user}/{file}', 'TeamFormController@downloadClientFile')->name('clientDownload');

TeamFormController

public function downloadClientFile($user, $file) {
        return response()->download(storage_path('app/upload/client/' . $user . '/' . $file));
    }

view

<a href="{{ route('clientDownload', ['user' => $teamform->forms->users->name, 'file' => $teamform->forms->upload]) }}">Download</a>
1 like
LadyDeathKZN's avatar

Thank you!!! you a god send, thank you for the patience and help!!!

1 like
schaeferalex's avatar

I suggest watching a few Laracast, especially https://laracasts.com/series/laravel-from-scratch-2018. They are a great starting point and will answer many of your questions in early episodes.

Laravel does most of the url generation for you. Have a look here: https://laravel.com/docs/5.8/urls#urls-for-named-routes In your view you would do something like this:

<a href="{{ route('clientDownload', ['file' => $filename]) }}">

Additionally you need to tell you controller to receive the url input

public function downloadClientFile($file) {
    return response()->download(storage_path('/app/upload/client/' . $file));
}

One word of warning: You will need to implement some sort of authentication and authorization. Otherwise it will be possible for any user to access any file in your storage by replacing the filename in the url.

https://laravel.com/docs/5.8/authentication https://laravel.com/docs/5.8/authorization

1 like

Please or to participate in this conversation.