• Laravel中的Storage::disk


    Laravel中的Storage::disk

    一、总结

    一句话总结:

    Storage的disk的路径和file的路径都是一回事,都是config/filesystems.php配置文件中disks

    比如$bool = Storage::disk('uploads')->put('/'.$data['pic_path'], file_get_contents($realPath));

    二、Laravel 文件上传,Storage::disk

    转自或参考:Laravel 文件上传,Storage::disk
    https://blog.csdn.net/vierhang/article/details/90376894

     
    1. 在config/filesystems.php文件中增加uploadsdisk驱动;

      例:

      'disks' => [
      
          'local' => [
              'driver' => 'local',
              'root' => public_path('app'),
          ],
          // 新建一个本地端uploads空间(目录) 用于存储上传的文件
          'uploads' => [
      
              'driver' => 'local',
      
              // 文件将上传到storage/app/uploads目录
             // 'root' => storage_path('app/uploads'),
      
              // 文件将上传到public/img 如果需要浏览器直接访问 请设置成这个
              'root' => public_path('img'),
          ],
      
    2. 在相应控制器中做文件上传逻辑

       if ($request->hasFile('picture')){
      			 //获取文件
                  $file = $request->file('picture');
                  $time = date('Ymd',time());
                  // 文件是否上传成功
                  if ($file->isValid()) {
                      // 获取文件相关信息
                      $originalName = $file->getClientOriginalName(); // 文件原名
                      $ext = $file->getClientOriginalExtension();     // 扩展名
                      $realPath = $file->getRealPath();   //临时文件的绝对路径
                      $type = $file->getClientMimeType();     // image/jpeg
                      // 上传文件
                      $filename = uniqid() . '.' . $ext;
                      $data['pic_path'] = 'blackvirus/'.$time.'/'.$filename;
                      // 使用我们新建的uploads本地存储空间(目录)
                      //这里的uploads是配置文件的名称
                      $bool = Storage::disk('uploads')->put('/'.$data['pic_path'], file_get_contents($realPath));
                       //判断是否创建成功
                      if (!$bool)
                      {
                          return $this->responseError('添加图片失败', $this->status_blackvirus_insert_img_error);
                      }
                  }
              }
      
     
  • 相关阅读:
    【面试】代码默写-DCL单例
    【状态机】SCXML2
    【面试】JVM
    【面试】HashMap
    读取resource下sql脚本并执行
    Maven 二进制资源文件(excel pdf...)部署出错,乱码的解决方案
    【JVM】java内存模型
    【Spring】源码二、IOC
    mapstruct 高级用法
    warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 6.0, but the range of supported deployment target versions is 8.0 to 13.2.99.
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/11973006.html
Copyright © 2020-2023  润新知