Laravel 5.6 Storage::download file not found

10,634

Solution was: return Storage::disk('public')->download($file->path);

Share:
10,634
darjus
Author by

darjus

Laravel/Symfony developer. Also codding with JS/Vue, node and GOlang

Updated on June 05, 2022

Comments

  • darjus
    darjus almost 2 years

    I'm using Laravel 5.6, filesystems.php config default.

    When file is uploaded I have record in my DB with path column where paths is recorded like - posts/p4NbvgcxHFcT10TwbS2UUJrajifPcqpN3i7iSyyk.png

    Symlink was created and files stored in public dir.

     $path = $file->store('posts', 'public');
    

    This path is recorder to DB, if I want to show e.g. image, I can access it by:

    {{ asset('/storage/'.$file->path) }}
    

    But when I try to force download it get exception - FileNotFoundException File not found at path:

    File download method:

      public function downloadFile($id){
        $file = File::find($id);
    
        //return Storage::download(storage_path()."/app/public/".$file->path, $file->original_name);
        //also tried like this
        return Storage::download(asset('storage/'.$file->path), $file->original_name);
    }
    

    App running on Windows OS

    • Lukáš Irsák
      Lukáš Irsák almost 6 years
      what returns $file->path property ? try to dump it in downloadFile method
    • brombeer
      brombeer almost 6 years
      {{ asset('/storage/'.$file->path) }} and asset('storage/'.$file->path) might not be same
    • darjus
      darjus almost 6 years
      dd( asset('/storage/'.$file->path), asset('storage/'.$file->path) ); result is same. $file->path return path like posts/p4NbvgcxHFcT10TwbS2UUJrajifPcqpN3i7iSyyk.png
  • darjus
    darjus almost 6 years
    Nope, same error. But storage_path('app/public/'.$file->path) gives correct path to the file. If i write to explorer path returned from this code I can access file, but Storage::download can't see this file.
  • Alexa
    Alexa over 2 years
    Gracias tu solución funcionó! Como comentario adicional, public deberá ser cambiado por el repositorio que se esté manejando como aws o digitalocean.