将多个图像上传到laravel中的aws

时间:2020-08-23 08:10:11

标签: laravel amazon-s3 file-upload

这是laravel项目,我想将多个图像上传到AWS s3服务器

这是刀片代码。

<div class="col-12">
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Add Multiple image...</span>
<input type="file" multiple="multiple" accept="image/*" class="imageupload1" name="uploadfile2[]" /><br/>
</span>
</div>

这些是控制器代码。

        if($request->hasfile($file_name))
        {
            $file = $request->file($file_name);
            $originalFileName = time().$file->getClientOriginalName();
            $filePath = $defaultPath.'/'.$originalFileName;

            $this->deleteimage($profile->thumb_img);
            $this->uploadimage($request, $file_name, $filePath);
        }

    public function uploadimage($request, $file_name, $defaultPath){

        $this->validate($request, [$file_name => 'required|image']);

        if($request->hasfile($file_name))
        {
            $file = $request->file($file_name);
            
            \Storage::disk('s3')->put($defaultPath, file_get_contents($file));
        }
    }

2 个答案:

答案 0 :(得分:0)

由于请求是作为数组传递的,因此您需要遍历每个迭代。


foreach ( $request->file($file_name) as $file )
{
     $originalFileName = time().$file->getClientOriginalName();
     $filePath = $defaultPath.'/'.$originalFileName;

     $this->deleteimage($profile->thumb_img);
     $this->uploadimage($request, $file_name, $filePath);       
}
           

答案 1 :(得分:0)

尝试如下。 让我知道。

if($request->hasfile($multi_file_name))
    $files = $request->file($multi_file_name);

    foreach($files as $imgfile) {
        $originalFileName = time().$imgfile->getClientOriginalName();
        $multi_filePath = $multi_defaultPath.'/'.$originalFileName;
        \Storage::disk('s3')->put($multi_filePath, file_get_contents($imgfile));
    }
}