public function actionUpload() { if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); $slider = Slider::findOne($post['sliderId']); $form = new ImageUploadForm(); $images = UploadedFile::getInstances($form, 'images'); foreach ($images as $k => $image) { $_model = new ImageUploadForm(); $_model->image = $image; if ($_model->validate()) { $path = \Yii::getAlias('@uploadsBasePath') . "/img/{$_model->image->baseName}.{$_model->image->extension}"; $_model->image->saveAs($path); // Attach image to model $slider->attachImage($path); } else { foreach ($_model->getErrors('image') as $error) { $slider->addError('image', $error); } } } if ($form->hasErrors('image')) { $form->addError('image', count($form->getErrors('image')) . ' of ' . count($images) . ' images not uploaded'); } else { Yii::$app->session->setFlash('image-success', Yii::t('app', 'Images successfully uploaded', ['Image' => Yii::t('app', 'Image'), 'images' => Yii::t('app', 'images'), 'n' => count($images)])); } return $this->redirect(['index?sliderId=' . $post['sliderId']]); } }
public function actionUpload() { // @todo Update code // http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/ if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); $gallery = Gallery::findOne(Yii::$app->session->get('gallery.gallery-id')); $form = new ImageUploadForm(); $images = UploadedFile::getInstances($form, 'images'); foreach ($images as $k => $image) { $_model = new ImageUploadForm(); $_model->image = $image; if ($_model->validate()) { $path = \Yii::getAlias('@uploadsBasePath') . "/img/{$_model->image->baseName}.{$_model->image->extension}"; $_model->image->saveAs($path); // Attach image to model $gallery->attachImage($path); } else { foreach ($_model->getErrors('image') as $error) { $gallery->addError('image', $error); } } } if ($form->hasErrors('image')) { // @todo Translate $response['message'] = count($form->getErrors('image')) . ' of ' . count($images) . ' images not uploaded'; } else { $response['message'] = Yii::t('app', '{n, plural, =1{Image} other{# images}} successfully uploaded', ['n' => count($images)]); } Yii::$app->response->format = Response::FORMAT_JSON; return $response; } }
/** * Shows profile settings form. * * @return string|\yii\web\Response */ public function actionProfile() { $model = $this->finder->findProfileById(\Yii::$app->user->identity->getId()); if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) { \Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } if ($model->load(\Yii::$app->request->post()) && $model->save()) { // Upload image $form = new ImageUploadForm(); $images = UploadedFile::getInstances($form, 'image'); $user = $model->user; // Remove old images if a new one is uploaded if ($images) { $user->removeImages(); foreach ($images as $k => $image) { $upload = new ImageUploadForm(); $upload->image = $image; if ($upload->validate()) { $path = \Yii::getAlias('@uploadsBasePath') . "/img/{$upload->image->baseName}.{$upload->image->extension}"; $upload->image->saveAs($path); // Attach image to model $user->attachImage($path); } else { foreach ($upload->getErrors('image') as $error) { $model->addError('image', $error); } } } /*if ($model->hasErrors('image')){ $model->addError( 'image', count($model->getErrors('image')) . Yii::t('app', 'of') . count($images) . ' ' . Yii::t('app', 'images not uploaded') ); } else { Yii::$app->session->setFlash('employee', Yii::t('app', '{n, plural, =1{Image} other{# images}} successfully uploaded', ['n' => count($images)])); }*/ } \Yii::$app->getSession()->setFlash('success', \Yii::t('app', '"{item}" has been updated', ['item' => Yii::$app->user->identity->username])); return $this->refresh(); } return $this->render('profile', ['model' => $model, 'image' => new ImageUploadForm()]); }
/** * Uploads and attaches an image * * @param string $name The name if the image in the uploaded files array * @param boolean $isMain The main image flag * @param string $identifier The image identifier */ public function uploadImageByName($name = '', $isMain = false, $identifier = '') { // Load the image $image = $this->getUploadedImageByName($name); if ($image) { $owner = $this->owner; // Delete current image if (!empty($identifier)) { $owner->removeImageByIdentifier($identifier); } else { $owner->removeImages(); } // Create uploadform and set the image $upload = new ImageUploadForm(); $upload->image = $image; // Validate the upload if ($upload->validate()) { // Upload the file $path = \Yii::getAlias('@uploadsBasePath') . "/img/{$upload->image->baseName}.{$upload->image->extension}"; $uploaded = $upload->image->saveAs($path); // Attach the file to the owner if ($uploaded) { $owner->attachImage($path, $isMain, $identifier); } } else { // Add upload errors to the owner foreach ($upload->getErrors('image') as $error) { $owner->addError('image', $error); } } } }