Home > Net >  How to retrieve image's name to store in image table after uploading on AWS S3 using Laravel?
How to retrieve image's name to store in image table after uploading on AWS S3 using Laravel?

Time:02-16

I am uploading images on AWS S3 using laravel8 and package (league/flysystem-aws-s3-v3) with following code.

    $image = $request->file('image');
    $data['image_name'] = time().'.'.$image->getClientOriginalExtension();
    $data['path'] = Storage::disk('s3')->put('images', $request->image);
    $data['ab'] = Storage::disk('s3')->url($data['path']);
    return $data;

After successful upload, Above code is returing

{
 "image_name": "1644919540.jpg",
 "path": "images/SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg",
 "ab": "https://abc-user-images.s3.ap-south-1.amazonaws.com/images/SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg"
}

I need to retrieve only the image's name in the path variable like

"path":"SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg"

  

              

CodePudding user response:

Need to use ltrim(string,charlist)

$image = $request->file('image');
$data['image_name'] = time().'.'.$image->getClientOriginalExtension();
$data['path'] = Storage::disk('s3')->put('images', $request->image);
$data['ab'] = Storage::disk('s3')->url($data['path']);
$image = ltrim($data['path'],"images/");
return $image;

The $image have what you need.

CodePudding user response:

$image_data['image_name'] = Str::of($data['path'])->substr(7); is also working

  • Related