Reputation: 398
I'm building a projectmanagement system, where users can upload file attachments to tasks. These attachments are stored in directory 'storage/app/public/projects//'. Uploading works like it should, in the right directory under the right ID.
However when I want to download said attachment, somehow the file can't be found. I've provided the code from my up- and download methods, as well as my 'local' driver configuration.
Driver configuration:
'local' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'storage'
],
Upload method:
$sExt = $request->file('attachment')->getClientOriginalExtension();
$sHashedName = substr(
sha1(str_replace(' ', '', pathinfo($request->file('attachment')->getClientOriginalName(), PATHINFO_FILENAME))
), 0, 14);
if ( ! Storage::disk('local')->exists($sHashedName . '.' . $sExt)) {
$request->file('attachment')->storeAs(
'/projects/'.$oTask->project->id, $sHashedName . '.' . $sExt, 'local'
);
}
$oTask->attachments()->create([
'name' => $request->name,
'location' => $sHashedName . '.' . $sExt,
'user_id' => Auth::id()
]);
Current download method:
public function downloadAttachment(Project $project, Task $task, Attachment $attachment)
{
var_dump(rtrim(env('APP_URL'), '/') . public_path('/projects/' . $project->id . '/' . $attachment->location));
if (rtrim(env('APP_URL'), '/') . public_path('/projects/' . $project->id . '/' . $attachment->location)) {
return response()->download(rtrim(env('APP_URL'), '/') . public_path('/projects/' . $project->id . '/' . $attachment->location));
} else {
return $this->redirectWithAlert(
'error',
'download',
'attachment',
$attachment->name,
'/projects/'.$project->id.'/tasks/'.$task->id
);
}
}
This was my last attempt before posting this question. I have also tried Storage::url('/projects/<project_id>/<filename>')
and a number of other things, but none work. I have linked a public storage directory according to the Laravel docs (with php artisan storage:link
). I'm probably missing something logical here, but I'm completely lost.
Upvotes: 2
Views: 941
Reputation: 2435
The public_path function refers to ./public/
, and not the storage disk 'public' that can be found in ./storage/app/public/
One way of solving this would be to replace public_path('/projects/'
with storage_path('app/public/projects/'
Upvotes: 6