dscs63759 2017-09-19 23:21
浏览 287
已采纳

Laravel - store()方法突然无法正常工作

I have a file upload method for a single image working properly, using the following code:

$file = $request->file('file');

if ($file && $file->isValid()) {
    $photo['size'] = $request->file('file')->getClientSize();
    $path = $request->file('file')->store($request->subdomain);
    $path = explode('/', $path);
    $photo['file'] = $path[1];
    $photo['cover'] = 1;
    $photo['gallery'] = $newGallery->id;
    $photo['uploaded_by'] = $user->id;
    Photo::create($photo);
}

$file is an instance of UploadedFile here, and the store() method works perfectly.

However, I needed to change the method to allow multiple files to be uploaded. The following adapted code nets the following error:

$photos = $request->files->all();

    foreach($photos as $photo) {
        foreach($photo as $p) {

            if($p->isValid()) {
                $path = $p->store($request->subdomain);
                $path = explode('/', $path);

                $newPhoto = [
                    'uploaded_by' => $user->id,
                    'file' => $path[1],
                    'size' => $p->getClientSize(),
                    'gallery' => $request->gallery,
                    'subdomain' => $request->subdomain,
                ];

                Photo::create($requestData);
            }
        }
    }

Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::store()

$p's dd output:

UploadedFile {#28 ▼
  -test: false
  -originalName: "Untitled.png"
  -mimeType: "image/png"
  -size: 18030
  -error: 0
  path: "/tmp"
  filename: "phpBDSWCR"
  basename: "phpBDSWCR"
  pathname: "/tmp/phpBDSWCR"
  extension: ""
  realPath: "/tmp/phpBDSWCR"
  aTime: 2017-09-19 20:19:57
  mTime: 2017-09-19 20:19:57
  cTime: 2017-09-19 20:19:57
  inode: 3014878
  size: 18030
  perms: 0100600
  owner: 1000
  group: 1000
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

Which is pretty strange, as Laravel should be using Illuminate\Http\UploadedFile that has the proper store() method (docs) against Symfony's class that doesn't have that method (docs)

Also, using Symfony's move() is way worse, as store() already saves the file with a generated filename and the extension, so I don't have to bother generating a random filename and guessing the extension, as it's not very reliable.

Bottom line is: why is it behaving like that, and how can I make it use the proper class instance?

  • 写回答

1条回答 默认 最新

  • douqiaolong0528 2017-09-20 00:18
    关注

    You can only use the store method you're using on a request instance.

    Maybe you could try doing something like this

    foreach($photo as $index => $p) {
        $request->file('files')[$index]->store();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计