Example #1
0
 /**
  * save file and insert into db
  * 
  * @return success: db id
  * @return error: false;
  **/
 public function saveFile()
 {
     if (!file_exists($this->file)) {
         return false;
     }
     $file = $this->file;
     $real_name = $file->getClientOriginalName();
     $file_path = GlobalFileFunc::getStorageRealPath();
     $save_name = GlobalFileFunc::getFileSaveName($real_name);
     $result = $file->move($file_path, $save_name);
     if (Auth::id()) {
         $user_id = Auth::id();
     } else {
         $user_id = null;
     }
     // echo GlobalFileFunc::getStorageRealPath() . GlobalFileFunc::getFileSaveName($real_name);
     if (!$file->getError()) {
         $createdFile = ['file_cd' => $this->file_cd, 'resource_id' => $this->resource_id, 'save_path' => GlobalFileFunc::getStorageRelativePath(), 'real_name' => $real_name, 'save_name' => $save_name, 'type' => $file->getClientMimeType(), 'remark' => Config::get('db_const.DB_FILE_CD_VALUE')[$this->file_cd], 'user_id' => $user_id, 'size' => $file->getClientSize()];
         $result = UploadFile::create($createdFile);
         if ($result->id) {
             return $result->id;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Example #2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     //
     // return 1;
     $request = new Request();
     $cat = $id;
     if (Auth::guest() || Auth::id() != $cat->created_user_id && Auth::user()->user_type === Config::get('db_const.DB_USERS_USER_TYPE_COMMON')) {
         abort(403);
     }
     $data = $request->input();
     $data['updated_user_id'] = Auth::id();
     $validator = Validator::make($data, ['name' => 'required|min:3', 'breed_id' => 'required']);
     return $request;
     return var_dump($request->hasFile('icon'));
     if ($request->hasFile('icon')) {
         $file = $request->file('icon');
         return $file;
         $icon_validator = ['image/jpeg', 'image/bmp', 'image/png', 'image/gif'];
         if (!in_array($file->getClientMimeType(), $icon_validator)) {
             return redirect('cat/create')->with('cat', $data)->withError('The icon must be a photo or gif.');
         }
         if ($validator->fails()) {
             return redirect('cat/create')->with('cat', $data)->withError('The icon must be a photo or gif.');
         }
         if (file_exists($file)) {
             $uploadFile = new UploadFile();
             $uploadFile->file = $file;
             $uploadFile->resource_id = $cat->id;
             $uploadFile->file_cd = Config::get('db_const.DB_FILE_FILE_CD_CAT_ICON_CODE');
             $result = $uploadFile->saveFile();
             if (!$result) {
                 return redirect('cat/create')->withError('Icon updating error, please try again.');
             }
         }
     }
     if ($cat->update($data)) {
         return redirect('/cat/' . $cat->id)->withSuccess('Cat has been updated.');
     } else {
         return redirect('/cat/' . $cat->id . "/edit")->withError('There are some errors, please try again.');
     }
 }