在Laravel 5.3中,我想设置一个公共可访问的上传目录,所以我在filesystems.php
配置文件中执行了此操作:
'disks' => [
...
'uploads' => [
'driver' => 'local',
'root' => public_path().'/uploads',
],
...
在FileController.php的store
函数中,我用它来创建文件:
// Send file to "uploads" drive, under "announcements" folder.
$path = $file->store('announcements', 'uploads');
// Create file in DB
$originalName = $file->getClientOriginalName();
$uploadedFile = File::create([
'name' => $originalName,
'path' => $path,
]);
此功能可下载文件:
public function publicDownload(Request $request)
{
$file = File::where('path', $request->p)->firstOrFail();
return response()->download('uploads/'.$request->p, $file->name);
}
这适用于开发环境(宅基地),但在生产环境(共享主机)中不起作用。代码或设置有什么问题?
Laravel.log说:
[2017-08-28 10:46:41] production.ERROR: exception 'Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException' with message 'The file "uploads/announcements/ee00ed45f3354894205c2ae47bb5b30a.pdf" does not exist' in /home3/xxx/app/vendor/symfony/http-foundation/File/File.php:37
答案 0 :(得分:1)
我刚刚注意到你的根在以下行中有问题,
'uploads' => [
'driver' => 'local',
'root' => public_path().'/uploads',
],
您已将root定义为..../public/uploads/
,因此您的完整文件路径将从..../public/uploads/
开始,并在目录..../public/uploads/upload/
中搜索并且不会退出的文件。
因此,只需将根目录更改为公共路径,
'root' => public_path(),
另一件事,
在下载中,您没有使用Storage Facade,因此您需要提供完整路径,例如
return response()->download(public_path().'/uploads/'.$request->p, $file->name);
希望你明白。