public function postUpload(Request $request) { $file = $request->file('image'); if (!$request->hasFile('image') || !$file->isValid()) { return redirect()->to('/'); } $path = $file->getRealPath(); $name = $file->getClientOriginalName(); $extension = $file->getClientOriginalExtension(); $size = $file->getSize() / 1024; $maxSize = 1024 * 4; $extList = ['jpg', 'jpeg', 'png', 'gif']; if ($size > $maxSize) { session()->flash('error', '超过文件大小限制:4 MB'); return redirect()->back(); } if (!in_array($extension, $extList)) { session()->flash('error', '仅允许 jpg, jpeg, png, gif 图片上传'); return redirect()->back(); } $dirName = date('Y/m'); $destinationPath = __DIR__ . '/../../../uploads/' . $dirName . '/'; $uuid = substr(md5(time() . rand(1, 9)), 8, 16); $fileName = md5($uuid . rand(1, 9)) . '.' . $extension; $file->move($destinationPath, $fileName); $image = new \Palette\Images(); $image->uuid = $uuid; $image->uid = Auth::user()->id; $image->name = $name; $image->dir = $dirName; $image->filename = $fileName; $image->ext = $extension; $image->size = $size; $image->save(); $user = \Palette\User::find(Auth::user()->id); $user->disk_used += $image->size; $user->save(); session()->flash('success', '图片上传成功'); return redirect()->to('img/list'); }
/** * Create a new user instance after a valid registration. * * @param array $data * @return User */ public function create(array $data) { return User::create(['email' => $data['email'], 'password' => bcrypt($data['password']), 'name' => $data['name'], 'disk_used' => '0', 'disk_total' => '512', 'group' => isset($data['group']) && $data['group'] === '1' ? '1' : '0']); }
/** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]); }