vendredi 4 mai 2018

Laravel 5.5: File download FileNotFoundException?

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.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2jqQINy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire