/**
  * Upload file to server.
  *
  * @param bool $printResponse
  *
  * @return array
  * @throws \yii\web\NotFoundHttpException
  */
 public function post($printResponse = true)
 {
     if (Yii::$app->request->get('delete') && ($id = Yii::$app->request->get('id'))) {
         return $this->delete($id, $printResponse);
     }
     $response = [];
     $this->_media = new Media();
     $this->_media->file = UploadedFile::getInstance($this->_media, 'file');
     if ($this->_media->file !== null && $this->_media->validate(['file'])) {
         if ($post_id = Yii::$app->request->get('post_id')) {
             $post = $this->findPost($post_id);
             $this->_media->media_post_id = $post->id;
         }
         $this->_media->media_title = $this->_media->file->baseName;
         $this->_media->media_mime_type = $this->_media->file->type;
         $this->handleFileUpload($this->_media->file);
         if ($this->_media->save()) {
             if ($this->_media->setMeta('metadata', $this->_meta)) {
                 $response = $this->generateResponse($this->_media);
             }
         }
     } else {
         $response[] = ['media_error' => $this->_media->getErrors('file'), 'media_filename' => isset($this->_media->file->name) ? $this->_media->file->name : null, 'media_file_size' => isset($this->_media->file->size) ? $this->_media->file->size : null];
     }
     $this->setResponse([$this->getOption('param_name') => [$response]]);
     return $this->getResponse($printResponse);
 }
 /**
  * Creates a new Media model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Media();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Esempio n. 3
0
 public function actionAjax()
 {
     $media = new Media();
     $uploaddir = 'media_file/';
     $uploadfile = $uploaddir . basename($_FILES['file']['name']);
     if (!empty($_FILES)) {
         if (copy($_FILES['file']['tmp_name'], $uploadfile)) {
             $media->link = $uploadfile;
             $media->save();
         }
     }
     $mediaAll = Media::find()->all();
     foreach ($mediaAll as $m) {
         echo "\n        <div class='mediaBox'>\n            " . Html::img(\yii\helpers\Url::base() . "/" . $m->link, ['width' => '150px', 'class' => 'imgPrev']) . "\n            <input type='hidden' value='" . \yii\helpers\Url::base(true) . "/" . $m->link . "'>\n        </div>";
     }
 }