Exemplo n.º 1
0
 /**
  * @POST("/uploading_data")
  * @param Request $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function uploading_data(Request $request)
 {
     $photos = $request->input('photos');
     $category_id = $request->input('category_id');
     if ($request->input('shop_id')) {
         $shop_id = $request->input('shop_id');
         $shop = Shops::find($shop_id);
         if ($shop && !$shop->canAddItem()) {
             return redirect()->route('shops.my');
         }
         foreach ($photos as $photo) {
             foreach ($photo as $item) {
                 $attachment = new Attachment();
                 $attachment->name = $item['aid'];
                 $attachment->path = $item['src_big'];
                 $attachment->url = $item['src_big'];
                 $attachment->save();
                 $shop_item = new ShopItems();
                 $shop_item->name = 'Название';
                 $shop_item->description = $item['text'];
                 $shop_item->shop_id = $shop_id;
                 $shop_item->category_id = $category_id;
                 if ($attachment) {
                     $shop_item->attachment()->associate($attachment);
                 }
                 $shop_item->save();
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Загрузка файлов
  */
 public function uploadFiles($dirName = null)
 {
     $ds = DIRECTORY_SEPARATOR;
     $ufolder = \Yii::getAlias('@app') . $ds . 'uploads' . $ds . 'temp' . $ds . $dirName . $ds;
     //директория откуда надо скопировать файлы
     if (file_exists($ufolder)) {
         $files = scandir($ufolder, 1);
         foreach ($files as $file) {
             //Get extension of file
             if ($file != '.' && $file != '..' && $file[0] != '.') {
                 $parts = explode('.', $file);
                 $ext = '.' . $parts[count($parts) - 1];
                 //расширение файла
                 $new_folder = \Yii::getAlias('@app') . $ds . 'uploads' . $ds . 'bid' . $ds . 'files' . $ds . $this->id . $ds;
                 //директория куда надо скопировать файлы
                 if (!file_exists($new_folder)) {
                     mkdir($new_folder, 0777);
                 }
                 $new_name = md5($file . time() . rand(0, 10000)) . $ext;
                 rename($ufolder . $file, $new_folder . $new_name);
                 $attachment = new Attachment();
                 $attachment->md5 = $new_name;
                 $attachment->name = $file;
                 $attachment->path = str_replace(\Yii::getAlias('@app'), '', \Yii::getAlias('@web') . $new_folder . $new_name);
                 $attachment->item_id = $this->id;
                 $attachment->save();
             }
         }
         rmdir($ufolder);
     }
 }
Exemplo n.º 3
0
 public function actionUpload($documentId)
 {
     $model = new Attachment();
     $model->documentId = $documentId;
     if ($model->load($_POST) && $model->save()) {
         \Yii::$app->response->format = Response::FORMAT_JSON;
         return ['files' => [$model->result()]];
     }
 }
 public function image(UploadedFile $file = null, Attachment $attachment = null)
 {
     if ($file == null) {
         return;
     }
     if ($attachment == null) {
         $attachment = new Attachment();
     }
     // dd($file->getClientOriginalExtension());
     $name = time() . '.' . $file->getClientOriginalExtension();
     $file->move(public_path() . $this->path . 'attachments/', $name);
     $attachment->name = $name;
     $attachment->path = $attachment->getUploadPath() . $name;
     //$attachment->user_id = Auth::user()->id;
     $attachment->save();
     return $attachment;
 }
Exemplo n.º 5
0
 public function attachAttachments($event)
 {
     $model = $this->owner;
     foreach ($this->objectTypes as $object_type) {
         $files = UploadedFile::getInstances($model, 'attachmentsUpload[' . $object_type . ']');
         if ($files) {
             foreach ($files as $file) {
                 $file_model = new Attachment();
                 $file_model->attach = $file;
                 $file_model->related_model = $model;
                 $file_model->table = $model->tableName();
                 $file_model->object_id = $model->id;
                 $file_model->object_type = $object_type;
                 $file_model->save();
             }
         }
     }
     $this->updateHasAttachmentsFlags();
 }
Exemplo n.º 6
0
 public function upload()
 {
     if ($this->validate()) {
         foreach ($this->files as $file) {
             $attachment = new Attachment();
             $attachment->document_id = $this->document_id;
             $attachment->name = \Yii::$app->security->generateRandomString() . '.' . $file->extension;
             $attachment->original_name = $file->baseName . '.' . $file->extension;
             $attachment->size = $file->size;
             if ($attachment->validate()) {
                 if ($file->saveAs(\Yii::$app->basePath . '/web/uploads/' . $attachment->name)) {
                     $attachment->save();
                 }
             }
         }
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 7
0
 /**
  * Загрузка файла. При успехе создание и сохранение модели Attachment
  * @return boolean
  */
 public function upload()
 {
     $ext = $this->file->extension;
     $origName = $this->file->baseName . '.' . $ext;
     $size = $this->file->size;
     $fileName = md5(uniqid()) . '.' . $ext;
     $path = $this->getUploadDir() . '/' . $fileName;
     if ($this->file->saveAs($path)) {
         // создаем и сохраняем модель Attachment
         $attachment = new Attachment();
         $attachment->document_id = $this->documentID;
         $attachment->origname = $origName;
         $attachment->filename = $fileName;
         $attachment->mimetype = FileHelper::getMimeType($path);
         $attachment->size = $size;
         if ($attachment->save()) {
             return true;
         } else {
             // если ничего не получилось, удаляем загруженный файл
             unlink($path);
         }
     }
     return false;
 }
Exemplo n.º 8
0
 /**
  * Execute the job.
  *
  * @return bool
  */
 public function handle()
 {
     $this->attachment->name = $this->request->input('name', $this->attachment->name);
     return $this->attachment->save();
 }