FileSystem:
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'shares' => [
'driver' => 'local',
'root' => public_path('shares'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
我的控制器:
public function store(Request $request) {
$rules = [
'user_id' => 'required|exists:users,id',
'patient_case_id' => 'required|exists:patient_cases,id',
'shared_with' => 'required|exists:users,id',
];
$this->validate($request, $rules);
$data = $request->all();
$data['shared_file'] = $request->file('file')->store('shares/'.$data["user_id"], $data["shared_file"]->getClientOriginalName());
$newShare = WayneRooney::create($data);
return $this->showOne($newShare, 201);
}
如果我将存储方法保留为空,它可以在公共路径中的共享文件夹中使用唯一ID工作并写入文件。但我想在shares / {USER_ID}文件夹下写下原始名称的文件。
我该怎么做?
答案 0 :(得分:3)
如果您不希望将文件名自动分配给存储的文件,则可以使用storeAs
方法,该方法接收路径,文件名和(可选)磁盘作为其参数:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
您也可以在Storage facade上使用putFileAs
方法,它将执行与上述示例相同的文件操作:
$path = Storage::putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
希望这可以解决您的问题!