/** * Creates a new Image model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Image(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function updateUpload() { $tokenImage = date("dmYHis") . bin2hex(openssl_random_pseudo_bytes(8)); if ($this->validate() and UploadedFile::getInstance($this, 'file')) { unlink('images/products/titles/' . $this->image); if ($this->file = UploadedFile::getInstance($this, 'file')) { $imageName = $this->file->baseName . '.' . $this->file->extension; $goodImage = $tokenImage . $imageName; $this->file->saveAs('images/products/titles/' . $goodImage); $this->image = $goodImage; } } // multiFiles upload if ($this->validate() and $this->multiFiles = UploadedFile::getInstances($this, 'multiFiles')) { foreach ($this->multiFiles as $file) { $goodImage = $tokenImage . $file->baseName . '.' . $file->extension; $file->saveAs('images/products/galleries/' . $goodImage); $image = new Image(); $image->name = $goodImage; $image->alt = $this->title; // $image->alt = $goodImage; $image->save(); $pi = new ProductImage(); $pi->product_id = $this->id; $pi->image_id = $image->id; $pi->save(); } } }