当前位置: 首页>>代码示例>>PHP>>正文


PHP File::uploaders方法代码示例

本文整理汇总了PHP中app\models\File::uploaders方法的典型用法代码示例。如果您正苦于以下问题:PHP File::uploaders方法的具体用法?PHP File::uploaders怎么用?PHP File::uploaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在app\models\File的用法示例。


在下文中一共展示了File::uploaders方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: handle

 /**
  * @param \App\Events\Files\Uploaded $event
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 public function handle(Uploaded $event)
 {
     $uploadUuid = $event->uploadUuid;
     $request = $event->request;
     /*
     |--------------------------------------------------
     | Move file to its final permanent destination.
     |--------------------------------------------------
     */
     $hash = hash_file('sha256', app('filestream')->getAbsolutePath($uploadUuid));
     $destination = $hash;
     if (config('filesystems.load_balancing.enabled')) {
         $config = config('filesystems.load_balancing');
         $folders = [];
         for ($i = 0; $i < $config['depth']; $i++) {
             $folders[] = substr($hash, -1 * ($i + 1) * $config['length'], $config['length']);
         }
         $destination = implode(DIRECTORY_SEPARATOR, array_merge($folders, [$hash]));
     }
     $filesystem = app('filesystem')->disk();
     !$filesystem->exists($destination) ? $filesystem->move($uploadUuid, $destination) : $filesystem->delete($uploadUuid);
     /*
     |---------------------------------
     | Check the tag and its limit.
     |---------------------------------
     */
     /** @var \App\Models\User $me */
     $me = app('sentinel')->getUser();
     $tag = $request->get('qqtag');
     $tagLimit = config('filesystems.allowed_tags_and_limits.' . $tag);
     if ($tagLimit > 0) {
         $allFilesWithSameTagBelongingToUser = $me->load(['files' => function (BelongsToMany $query) use($tag) {
             $query->wherePivot('tag', '=', $tag);
         }])->files;
         if (($numberOfFilesToDeleteToComplyWitTagLimit = $allFilesWithSameTagBelongingToUser->count() - $tagLimit + 1) > 0) {
             while ($numberOfFilesToDeleteToComplyWitTagLimit > 0) {
                 $pivotToDelete = $allFilesWithSameTagBelongingToUser->shift();
                 app('filestream')->deleteFile($pivotToDelete->hash, $tag);
                 $numberOfFilesToDeleteToComplyWitTagLimit--;
             }
         }
     }
     /*
     |------------------------------------
     | Persist file record in database.
     |------------------------------------
     */
     $uploadedFile = new SymfonyFile(app('filestream')->getAbsolutePath($destination));
     if (!($file = File::find($hash = $uploadedFile->getFilename()))) {
         $file = new File();
         $file->hash = $hash;
         $file->disk = 'local';
         $file->path = trim(str_replace(config('filesystems.disks.local.root'), '', $uploadedFile->getPathname()), DIRECTORY_SEPARATOR);
         $file->mime = $uploadedFile->getMimeType();
         $file->size = $uploadedFile->getSize();
         $file->save();
     }
     $me->files()->newPivotStatement()->whereTag($tag)->whereFileHash($hash)->delete();
     $file->uploaders()->attach([$me->getUserId() => ['uuid' => $request->get('qquuid'), 'original_client_name' => $request->get('qqfilename'), 'tag' => $tag]]);
     return response()->json(['success' => true, 'hash' => $file->hash])->setStatusCode(IlluminateResponse::HTTP_CREATED);
 }
开发者ID:audithsoftworks,项目名称:basis,代码行数:66,代码来源:PersistUploadedFile.php


注:本文中的app\models\File::uploaders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。