在foreach循环中的Laravel getClientOriginalName

时间:2016-05-12 09:06:49

标签: php laravel foreach laravel-5.1

我无法调用此方法

getClientOriginalName 

在这个循环中

        foreach ($request->file('images', []) as $scenesImagesData) {
            $scenes_images = new ScenesImages($scenesImagesData);
            $scenes_images->product()->associate($product);
            $fileName  = $scenesImagesData->getClientOriginalName();
            $filePath  = $targetPath . '\\' . $fileName;
            $scenes_images->save();
        }

我收到以下错误:

Call to a member function getClientOriginalName() on a non-object

如果我dd($ scenesImagesData)我得到了这个:

array:1 [▼
  "scenes_images" => UploadedFile {#30 ▼
    -test: false
    -originalName: "dog.jpg"
    -mimeType: "image/jpeg"
    -size: 434516
    -error: 0
  }
]

我选择的是哪个文件。

因此我确实有

enctype="multipart/form-data"

以我的形式。

如果我print_r($scenesImagesData);

我得到以下信息:

  

数组([scenes_images] =>   Symfony \ Component \ HttpFoundation \ File \ UploadedFile对象(   [test:Symfony \ Component \ HttpFoundation \ File \ UploadedFile:private] =>   [ORIGINALNAME:Symfony的\分量\ HttpFoundation \文件\ UploadedFile的:私人]   => dog.jpg [mimeType:Symfony \ Component \ HttpFoundation \ File \ UploadedFile:private]   => image / jpeg [size:Symfony \ Component \ HttpFoundation \ File \ UploadedFile:private] =>   434516   [错误:Symfony \ Component \ HttpFoundation \ File \ UploadedFile:private] =>   0 [pathName:SplFileInfo:private] => C:\ XAMPP \ tmp目录\ php786B.tmp   [fileName:SplFileInfo:private] => php786B.tmp))

2 个答案:

答案 0 :(得分:1)

所以,你有一个数组内的对象,所以试试这个:

$name = $scenesImagesData['scenes_images']->getClientOriginalName();

答案 1 :(得分:0)

尝试

$scenesImagesData["scenes_images"]->UploadedFile()->originalName

$scenesImagesData["scenes_images"]->getClientOriginalName();

其中一个或两个都应该有效。