public function upload(Request $request) { $file = $request->file('image'); if (Input::hasFile('image')) { $destinationPath = '/storage/avatars/'; $size = $file->getSize(); if ($size <= 41943040) { $fileName = Auth::user()->id . '.' . 'jpg'; $file->move(base_path() . $destinationPath, $fileName); if (DB::table('photos')->where('user_id', Auth::user()->id)->value('user_id') == Auth::user()->id) { } else { $photos = new Photo(); //обращается к контроллеру??? $photos->user_id = Auth::user()->id; $photos->url = 'storage/avatars/' . $fileName; //ссылки на картинки $photos->main = 1; $photos->save(); } return view('home'); } else { return view('errors.bigFile'); } } else { return view('errors.noUploadFile'); } }
public function actionUpload() { Yii::$app->response->format = Response::FORMAT_JSON; $result = []; $files = UploadedFile::getInstances(new File(), 'uploadedFiles'); foreach ($files as $index => $file) { $model = new File(); $model->uploadedFiles = [$file]; $model->user_id = Yii::$app->user->id; $model->storage = File::STORAGE_OS; $model->extension = $file->extension; $model->type = File::defineType($model->extension); if ($model->upload()) { $item = ['id' => $model->id, 'url' => $model->getUrl(), 'type' => $model->getMimeType()]; if ($releaseId = Yii::$app->request->get('rid')) { $release = Release::findOne($releaseId); if ($release && $release->artist->user_id == Yii::$app->user->id) { if ($model->type == $model::TYPE_IMAGE) { $cover = new Cover(['release_id' => $release->id, 'file_id' => $model->id, 'is_main' => false]); $cover->save(); $item['image_id'] = $cover->id; } elseif ($model->type == $model::TYPE_AUDIO) { $track = new Track(['release_id' => $release->id, 'original_name' => $file->baseName, 'number' => $release->getTracks()->count() + 1, 'file_id' => $model->id]); $track->save(); $item['track_id'] = $track->id; $item['comname'] = $track->getComname(); $item['number'] = $track->number; } } } if ($artistId = Yii::$app->request->get('aid')) { $photo = new Photo(['artist_id' => (string) $artistId, 'file_id' => $model->id, 'is_main' => false]); $artist = Artist::findOne($artistId); if ($artist && $artist->user_id == Yii::$app->user->id) { $photo->save(); $item['image_id'] = $photo->id; } } // if ($model->type == $model::TYPE_AUDIO) { // $getID3 = new getID3(); // $tags = $getID3->analyze($model->getPath(true))['tags']['id3v2']; // $item['meta'] = [ // 'title' => $tags['title'][0], // 'number' => explode('/', $tags['track_number'][0])[0], // 'disc' => explode('/', $tags['part_of_a_set'][0])[0], // 'lyric' => $tags['unsynchronised_lyric'][0], // 'info' => $tags['comment'][0], // ]; // } if ($model->type == $model::TYPE_AUDIO) { $item['meta'] = ['title' => $file->baseName, 'number' => $index + 1, 'disc' => '', 'lyric' => '', 'info' => '']; } $result[] = $item; } } return $result; }
/** * Creates a new Photo model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Photo(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $file = $request->file('pic'); $uploader = new FileUpload(); $uploader = $uploader->uploadPic($file, 'insertions/'); $post = new Insertion(); $post->title = $request->title; $post->description = $request->description; $post->author_id = Auth::user()->id; $post->save(); $photos = new Photo(); $photos->user_id = Auth::user()->id; $photos->insertion_id = $post->id; $photos->url = '/storage/insertions/' . $uploader; //?????? ?? ???????? $photos->main = 1; $photos->save(); return redirect()->action('Insertions\\PostsController@index'); }
static function multiple_uploadPic($path) { // getting all of the post data $files = Input::file('images'); // Making counting of uploaded images $file_count = count($files); // start count how many uploaded $uploadcount = 0; if ($uploadcount < 3) { foreach ($files as $file) { $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc' $validator = Validator::make(array('file' => $file), $rules); if ($validator->passes()) { $destinationPath = '/storage/' . $path; $fileName = DB::table('photos')->max('id') + 1 . '-' . $file->getClientOriginalName(); /*(DB::table('photos')->max('id')) + 1 */ $file->move(base_path() . $destinationPath, $fileName); $photos = new Photo(); if ($uploadcount == 0) { $photos->advert_id = DB::table('advert')->max('id') + 1; $photos->main = 1; } else { $photos->advert_id = DB::table('advert')->max('id') + 1; $photos->main = 0; } $photos->url = '/storage/adverts/' . $fileName; //ссылки на картинки $photos->save(); $uploadcount++; } } } /* if($uploadcount == $file_count){ return $fileName[]; } else { return Redirect::to('posts.index')->withInput()->withErrors($validator); }*/ }
public function actionUpload() { $file = UploadedFile::getInstanceByName('file'); if ($file->hasError) { throw new ServerErrorHttpException('Ошибка при загрузке'); } /** @var Users $user */ $user = Users::findOne(Yii::$app->request->post('user_id')); if (!$user) { throw new NotFoundHttpException('Не наден пользователь'); } $fileName = $file->name; $uploadDir = Yii::getAlias('@webroot/images/' . $user->id . '/'); FileHelper::createDirectory($uploadDir); // Thumbnail $img = Image::getImagine()->open($file->tempName); $size = $img->getSize(); $ratio = $size->getWidth() / $size->getHeight(); $width = Yii::$app->params['thumbnailWidth']; $height = round($width / $ratio); // Save thumbnail Image::thumbnail($file->tempName, $width, $height)->save($uploadDir . 'thumbnail_' . $fileName, ['quality' => 70]); // Save image if (file_exists($uploadDir . $fileName)) { $fileName = $file->baseName . '-' . uniqid() . '.' . $file->extension; } if (!$file->saveAs($uploadDir . $fileName)) { throw new ServerErrorHttpException('Ошибка при сохранении файла'); } // Save model $model = new Photo(); $model->user_id = $user->id; $model->project_id = $user->project_id; $model->filename = $fileName; $model->save(); $response = ['filename' => $fileName]; return Json::encode($response); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { try { $this->validate($request, ['description' => 'max:255', 'picture' => 'required|mimes:jpeg,png']); } catch (\Exception $e) { return redirect()->back(); } $uploadedFile = Input::file('picture'); $destinationPath = 'photos'; // upload path $extension = $uploadedFile->guessExtension(); $fileName = uniqid() . '.' . $extension; $fullPath = $destinationPath . DIRECTORY_SEPARATOR . $fileName; Storage::disk('s3')->put($fullPath, file_get_contents($uploadedFile->getRealPath())); $photo = new Photo(); if ($description = Input::get('description')) { $photo->description = $description; } $photo->mime = $uploadedFile->getClientMimeType(); $photo->filename = $fullPath; $photo->album_id = Input::get('album_id'); $photo->save(); return redirect()->route('album.show', [$photo->album_id]); }
/** * @param $id * @param AdminFormRequest $request * @return \Illuminate\Http\RedirectResponse */ public function patchUpdate($id, AdminFormRequest $request) { $user = User::findOrFail($id); $file = $request->file('image'); $msg = 'Update user failed'; if (!empty($file)) { //deleting old file $objPhoto = Photo::find($user->photo_id); $photoName = base_path() . $this->storagePath . $objPhoto->photo_name; @unlink($photoName); $objPhoto->delete(); //writing new file $extName = $file->getClientOriginalExtension(); //Generating image name $imageName = uniqid(rand()) . ".{$extName}"; $image = new Photo(); $image->photo_name = $imageName; $image->save(); //moving file $fullPath = base_path() . $this->storagePath; $file->move($fullPath, $imageName); //save all with relations if ($image->users()->save($user)) { $msg = 'User was update'; } } else { $user->update($request->all()); if ($user->save()) { $msg = 'User was updated'; } } return redirect()->action('AdminController@getIndex')->with('message', $msg); }
public function actionHome() { $request = Yii::$app->request; $page_id = $request->get('page_id'); $photo = Photo::find()->where(['page_id' => $page_id])->orderBy('create_date desc')->one(); $request = Yii::$app->request; $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); if ($model->upload()) { $date = date("Ymd", time()); foreach ($model->imageFiles as $key => $image) { $path = '/uploads/picnews/' . $date . '/'; $photo = new Photo(); $photo->path = $path . $image->baseName . '.' . $image->extension; $photo->page_id = $page_id; $photo->parent_id = 0; $photo->create_date = time(); $photo->update_date = time(); $photo->save(); } return $this->redirect(['home', 'page_id' => $page_id]); } } else { return $this->render('home', ['model' => $model, 'photo' => $photo]); } }
public static function addPostAttachmentPhoto($data, $post_id) { foreach ($data as $picture) { $photo = new Photo(); $photo->user_id = 0; $genFileName = Yii::$app->security->generateRandomString(20); $img = new Image($genFileName, ImageTypes::PostPhoto(), new ImgMediaLoc(), $picture); $img->save(); $photo->filename = $genFileName; $photo->type = "postPhoto"; PostsService::addPostAttachmentPhoto($post_id, $genFileName); $photo->save(); } }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Request $request, $id) { $this->validate($request, ['goods_name' => 'required|max:255', 'store_price' => 'required|numeric|min:0']); $goods = Goods::find($id); $goods->goods_name = $request->input('goods_name'); $goods->store_price = $request->input('store_price'); //$post->slug = Str::slug(Input::get('title')); $goods->cat_id = Input::get('cat_id'); $goods->content = Input::get('content'); $goods->user_id = Auth::user()->id; $goods->type_id = Input::get('type_id'); //to-do $goods->brand_id = 1; //to-do if ($file = Input::file('thumb')) { $allowed_extensions = ["png", "jpg", "gif"]; if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) { return ['error' => 'You may only upload png, jpg or gif.']; } $fileName = $file->getClientOriginalName(); $extension = $file->getClientOriginalExtension() ?: 'png'; $folderName = 'uploads/images/' . date("Ym", time()) . '/' . date("d", time()); $destinationPath = public_path() . '/' . $folderName; $safeName = str_random(10) . '.' . $extension; $file->move($destinationPath, $safeName); $goods->thumb = $folderName . '/' . $safeName; } //开始事务 DB::beginTransaction(); try { if ($goods->save()) { $thumbUrls = Input::get('thumbUrls'); $originalUrls = Input::get('originalUrls'); if (is_array($originalUrls)) { foreach ($originalUrls as $k => $originalUrl) { $photo = new Photo(); $photo->goods_id = $goods->id; $photo->original_url = $originalUrls[$k]; $photo->thumb_url = $thumbUrls[$k]; $photo->save(); } } //商品关联属性 //删除重复,goods_id,attr_id,attr_value $attrIdList = Input::get('attr_id_list'); $attrValueList = Input::get('attr_value_list'); $attrPriceList = Input::get('attr_price_list'); $attrs = array_map(null, $attrIdList, $attrValueList, $attrPriceList); for ($i = 0; $i < count($attrs); $i++) { for ($j = $i + 1; $j < count($attrs); $j++) { if ($attrs[$i][0] == $attrs[$j][0] && $attrs[$i][1] == $attrs[$j][1]) { unset($attrs[$i]); } } } //数据标记 foreach ($attrs as $_k => $_v) { if (!GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->first()) { $attrs[$_k]['sign'] = 'insert'; } elseif (GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->first()) { $attrs[$_k]['sign'] = 'update'; } } //数据标记 foreach ($goods->goodsAttrs as $v) { $isDel = true; foreach ($attrs as $_v) { if ($v['attr_id'] == $_v[0] && $v['attr_value'] == $_v[1]) { $isDel = false; } } if ($isDel) { $attrs[] = [$v['attr_id'], $v['attr_value'], 'sign' => 'delete']; } } //数据操作 foreach ($attrs as $_v) { if ($_v['sign'] == 'insert') { $attr = new GoodsAttr(); $attr->goods_id = $goods->id; $attr->attr_id = $_v[0]; $attr->attr_value = $_v[1]; $attr->attr_price = $_v[2]; //如果为字符串attr_price存入数据库为0 if ($attr->goods_id && $attr->attr_id && $attr->attr_value) { //删除存在的 //$attrs::where(['goods_id'=>$attr->goods_id, 'attr_id'=>$attr->attr_id, 'attr_value'=>$attr->attr_value]); //保存 $attr->save(); } } elseif ($_v['sign'] == 'update') { GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->update(['attr_price' => $_v[2]]); } elseif ($_v['sign'] == 'delete') { $attr = GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->delete(); } } DB::commit(); return Redirect::to('admin/goods'); } else { return Redirect::back()->withInput()->withErrors('保存失败!'); } } catch (\Exception $e) { DB::rollBack(); throw $e; } }