【发布时间】:2021-04-21 12:52:18
【问题描述】:
当我将文件上传(实际移动)到 laravel 8 中的公用文件夹时,它们被存储为 .tmp 文件,当我尝试将文件存储在 storage 文件夹中时,它说无法打开流:权限被拒绝。我应该启用某种写权限吗?还是我在代码中遗漏了什么?
if($request->file('file')) {
$uniqueFileName = uniqid() . $request->file('file')->getClientOriginalName();
$post = new Post();
$post->name = request('name');
$post->email = request('email');
$post->doc = $uniqueFileName;
$request->file('file')->move(public_path('uploads/files',$uniqueFileName));
//I also tried to store files here, but says "failed to open stream: permission denied"
//$request->file('file')->store('uploads');
$post->save();
return redirect()->back();
}
【问题讨论】:
-
将公用文件夹更改为 777
-
为什么要将文件存储在公共目录中?推荐的方法是将文件存储在存储目录中,然后公开符号链接(docs. here laravel.com/docs/8.x/structure#the-storage-directory)。 777 权限转到“存储”文件夹,而不是“公共”