PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] How to retrieve image's name to store in image table after uploading on AWS S3 using Laravel?

 March 07, 2022     amazon-s3, amazon-web-services, laravel, laravel-8     No comments   

Issue

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"

  

              

Solution

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.



Answered By - sujit prasad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing