Пример #1
0
 /**
  * Creates a new Cars model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $fileName = 'file';
     $imgs = \yii\web\UploadedFile::getInstancesByName($fileName);
     $model = new Cars();
     // echo getcwd();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         foreach ($imgs as $img) {
             $mysqlName = time() . '_' . $img->name;
             $url = getcwd() . '/src/upload/' . $mysqlName;
             $kaboom = explode(".", $img->tempName);
             // Split file name into an array using the dot
             $fileExt = end($kaboom);
             // Now target the last array element to get the file extension
             self::ak_img_resize($img->tempName, $url, 600, 394, $fileExt);
             //if (rename($img->tempName, $url));
             chmod($url, 0755);
             $photo = new Photos();
             $photo->car_id = $model->id;
             $photo->url = $mysqlName;
             $photo->save();
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Пример #2
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(ThemXeRequest $request)
 {
     $car = new Cars();
     $car->hang_id = $request->hang_name;
     $car->ten_xe = $request->ten_xe;
     $car->giamuaxe = str_replace('.', '', $request->giamuaxe);
     //dd($car->giamuaxe); die();
     $car->sodangky_xe = $request->sodangky_xe;
     $car->color = $request->color;
     $car->socho_xe = $request->socho_xe;
     $car->tai_xe_id = $request->tai_xe_id;
     $file = $request->file('url_hinhxe');
     $destinationPath = base_path() . "/public/user/images/";
     $function = new MyFunction();
     $url_hinhxe = $function->stripUnicode(basename($file->getClientOriginalName()));
     //dd($car->ngaysanxuat); die();
     $car->url_hinhxe = $url_hinhxe;
     $fileName = $destinationPath . $url_hinhxe;
     //dd($file); die();
     if ($request->hasFile('url_hinhxe')) {
         if ($file->isValid()) {
             $file->move($destinationPath, $fileName);
         }
     }
     $car->ngaysanxuat = $function->changedatetimeformat($request->ngaysanxuat);
     $car->ngaydangkiem = $function->changedatetimeformat($request->ngaydangkiem);
     $car->save();
     $order = new Vote(['sovotes' => 0, 'tongdiem' => 0]);
     $car->vote()->save($order);
     return redirect()->route('themxe')->with(['flash_level' => 'success', 'flash_message' => 'Đã thêm dữ liệu thành công']);
 }