Laravel 5.4 存储:下载文件。文件不存在,但显然存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42116349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 Storage : downloading files. File does not exist, but clearly it does
提问by newGuy
I have been going round and round with this. I have uploads working with the follow:
我一直在绕圈子。我上传了以下内容:
public function store(Tool $tool)
{
If(Input::hasFile('file')){
$file = Input::file('file');
$name = $file->getClientOriginalName();
$path=Storage::put('public',$file); //Storage::disk('local')->put($name,$file,'public');
$file = new File;
$file->tool_id = $tool->id;
$file->file_name = $name;
$file->path_to_file = $path;
$file->name_on_disk = basename($path);
$file->user_name = \Auth::user()->name;
$file->save();
return back();
}
however when I try to download with:
但是当我尝试下载时:
public function show($filename)
{
$url = Storage::disk('public')->url($filename);
///$file = Storage::disk('public')->get($filename);
return response()->download($url);
}
I get the FileNotFound exception from laravel However, if I use this instead:
我从 laravel 得到 FileNotFound 异常但是,如果我改用它:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
I get
我得到
FileNotFoundException in File.php line 37: The file "use calib;
insert into notes(
tool_id
,user_id
,note
,created_at
,updated_at
) VALUES(1,1,'windows server 2008 sucks',now(),now());" does not exist
File.php 第 37 行中的 FileNotFoundException:文件“use calib;
insert into notes(
tool_id
,user_id
,note
,created_at
,updated_at
) VALUES(1,1,'windows server 2008ucks',now(),now());" 不存在
which is the actual content of the file...
这是文件的实际内容...
It can obviously find the file. but why wont it download?
它显然可以找到该文件。但为什么不下载?
回答by Paras
Try this:
尝试这个:
return response()->download(storage_path("app/public/{$filename}"));
回答by rap-2-h
Replace:
代替:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
With:
和:
return response()->download(storage_path('app/public/' . $filename));
response()->download()
takes a path to a file, not a file content. More information here: https://laravel.com/docs/5.4/responses#file-downloads
response()->download()
获取文件的路径,而不是文件内容。更多信息在这里:https: //laravel.com/docs/5.4/responses#file-downloads
回答by Bruce Tong
If any one still could not find their file even though the file clearly exists then try
如果任何人仍然无法找到他们的文件,即使该文件明确存在,请尝试
return response()->file(storage_path('/app/' . $filename, $headers));
It could be due to a missing directory separator or it isn't stored inside the public folder.
这可能是由于缺少目录分隔符或它没有存储在公用文件夹中。