/** * Creates a new Material model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Material(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Material model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Material(); $arr['Material'] = Yii::$app->request->post(); if ($model->load($arr) && $model->save()) { return Json::encode($model); } else { return Json::encode(['message' => 'Record cound\'t be saved', 'errors' => $model->errors]); } }
private function upload($post = []) { if (empty($post) || empty($_FILES['FileData'])) { throw new NotFoundHttpException(Yii::t('yii', '没有上传文件')); } //实例化上传组件 $uploader = Yii::createObject(['class' => 'common\\components\\Uploader', 'savePath' => '@upload/' . $post['type'] . '/', 'allowExts' => $this->allowTypes[$post['type']], 'maxSize' => $this->forevelMaterialSizes[$post['type']]]); //执行上传 $ret = $uploader->upload($_FILES['FileData']); if ($ret === false) { throw new NotFoundHttpException(Yii::t('yii', $uploader->errorMsg)); } //用返回的信息拼接图片路径,将图片上传到微信 $filePath = $uploader->uploadFileInfo['savepath'] . $uploader->uploadFileInfo['savename']; if (class_exists('\\CURLFile')) { $data = ['media' => new \CURLFile($filePath)]; } else { $data = ['media' => '@' . $filePath]; } $isVideo = false; $videoInfo = []; //判断是不是视频 if ($post['type'] == 'video') { $isVideo = true; $videoInfo = ['title' => $post['title'], 'introduction' => $post['introduction']]; } $uploadRet = $this->wechat->uploadForeverMedia($data, $post['type'], $isVideo, $videoInfo); if ($uploadRet == false || empty($uploadRet)) { throw new NotFoundHttpException(Yii::t('yii', '上传素材到微信失败')); } //将素材信息保存到数据库,以方便展示 $model = new Material(); $model->public_id = $this->pid; $model->name = $_FILES['FileData']['name']; //原始文件名 $model->filepath = $post['type'] . '/' . $uploader->uploadFileInfo['secondfilePath']; $model->filename = $uploader->uploadFileInfo['savename']; $model->type = $post['type']; //获取图片大小 if ($post['type'] == 'image' || $post['type'] == 'thumb') { $picSize = getimagesize($filePath); $model->imgwidth = $picSize[0]; $model->imgheight = $picSize[1]; } $model->filesize = $_FILES['FileData']['size']; $model->create_time = time(); $material_id = 0; if ($model->save()) { $material_id = $model->id; } $uploadRet['material_id'] = $material_id; return $uploadRet; }