Php 使用laravel强制下载文件

Php 使用laravel强制下载文件,php,laravel,download,laravel-5,laravel-response,Php,Laravel,Download,Laravel 5,Laravel Response,所以我使用的是Laravel5,我试图强制下载一个选定的文件,但什么也没发生 public function downloadUserFile(){ $result = $_POST['filename']; $entry = File::where('filename', $result)->firstOrFail(); $file = Storage::disk()->get($entry->filePath);

所以我使用的是Laravel5,我试图强制下载一个选定的文件,但什么也没发生

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);
}
而且响应标题似乎还可以

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

你知道怎么了吗?

我想问题出在路径上

默认情况下,在
config/filesystems.php
中,本地路径是这样定义的:
storage\u path('app')
,然后您进入下载路径:
storage\u path($entry->filePath)
(此处不包括
app

你应该做的是改变:

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


我已经改了。但我没有向视图传递任何信息。这可能是问题所在吗?你指的是什么视图?你们不使用这里的任何视图,点击后你们可以简单的强制浏览器下载文件,就这样,但什么也没发生。没有强制下载
return response()->download(storage_path('app/'.$entry->filePath), $result, $headers);