Kliptu
Kliptu

Reputation: 199

Upload image and extract filename instead of file path

I am using following code to upload thumbnail in "storage/app/public/websites"

 $path = $request->file('thumbnail')->store('public/websites');

It is working fine and upload images to "websites" directory but problem is it returns the actual path e.g. websites/r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg

i only want to save image name e.g. r63mAKN1kil3BIwvwwRevOv93MgWQFme39BwH8ZV.jpeg in database table.

By default Laravel generates Unique ID for image name. Is there way to return only filename instead of path ?

Upvotes: 0

Views: 1797

Answers (4)

Elmar
Elmar

Reputation: 68

$info = pathinfo( $url );

$contents = ( new \GuzzleHttp\Client() )->get( $url, [ 'verify' => true ] )->getBody()->getContents();
$file = str_finish(sys_get_temp_dir(), '/') . $info[ 'basename' ];

\File::put( $file, $contents );

$ext = ( new UploadedFile( $file, $info[ 'basename' ] ) )->guessExtension();

Upvotes: 0

EldinPHP
EldinPHP

Reputation: 13

This is what you need

$extension = $request->image->getClientOriginalExtension();
$image_name = str_replace(' ', '', trim($request->model) . time() . "." . $extension);

and to move the image to your desired folder use:

  $image_path = $request->image->move(public_path('images'), $image_name);

Upvotes: 0

ibrahimSmuhmmad
ibrahimSmuhmmad

Reputation: 36

This is the hash name, so, first I think you need to separate your steps.

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

$path = $file->store('public/websites');

when you need to add file name you can use $file->hashName();

Upvotes: 2

Teodor
Teodor

Reputation: 43

You can use the basename function

http://php.net/manual/en/function.basename.php

$filename = basename($path);

Upvotes: 0

Related Questions