Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force download file with laravel

So i'm using Laravel 5 and I tried to force download a selected file but nothing happens.

public function downloadUserFile(){
        $result = $_POST['filename'];
        $entry = File::where('filename', $result)->firstOrFail();
        $file = Storage::disk()->get($entry->filePath);
        $headers = array('Content-Type' => $entry->mimetype);
        return response()->download(storage_path($entry->filePath), $result, $headers);
}

and the response headers seem to be ok

Accept-Ranges: none
Cache-Control: public
Connection: Keep-Alive
Content-Disposition: attachment; filename="SIGNAL - Nadezdata.mp3"
Content-Length: 4205059
Content-Type: audio/mpeg

Do you know what's wrong?

like image 692
Alex Avatar asked Nov 08 '22 23:11

Alex


1 Answers

I think the problem is in paths.

By default in config/filesystems.php local path is defined this way: storage_path('app') and you pass into download the following path: storage_path($entry->filePath) (no app included here).

What you should do is changing:

return response()->download(storage_path($entry->filePath), $result, $headers);

into:

return response()->download(storage_path('app/'.$entry->filePath), $result, $headers);
like image 76
Marcin Nabiałek Avatar answered Nov 14 '22 23:11

Marcin Nabiałek