validate( rules: [ 'file' => [ 'bail', 'required', File::types(['image/jpeg', 'image/png']) ->extensions(['jpg', 'jpeg', 'png']) ->max(20 * 1024), ], ], attributes: [ 'file' => '文件', ], ); /** @var \Illuminate\Http\UploadedFile */ $file = $request->file('file'); if ($path = $file->storeAs(date('Ymd'), $this->filename($file))) { return [ 'url' => Storage::url($path), ]; } throw new RuntimeException('上传失败,请重试'); } protected function filename(UploadedFile $file): string { $hash = Str::random(40); $extension = ''; if ($originalExtension = $file->getClientOriginalExtension()) { $extension = '.'.$originalExtension; } elseif ($guessExtension = $this->guessExtension()) { $extension = '.'.$guessExtension; } return $hash.$extension; } }