Theodoros80
Theodoros80

Reputation: 796

get Laravel's store function unique ID

From the Laravel's documentation (file uploads), the store function will generate a unique ID to serve as the file name. My code for uploading and storing the file is:

$path = $request->file('headerimg')->store('images');

Now $path will look like this: images/sCzDxJBUnkveRLdzfhsIA7hAS8RVfPDXuJjBPgzM.jpeg.

How can i get the filename only(sCzDxJBUnkveRLdzfhsIA7hAS8RVfPDXuJjBPgzM.jpeg) so I can use it let's say in a database, because right now all i have is the full path.

I can do it with PHP's explode but this is not the case.

BTW, using getClientOriginalName() will return the original filename, which is redundant since laravel is storing files with a unique ID.

Thanks.

Upvotes: 2

Views: 1128

Answers (2)

Theodoros80
Theodoros80

Reputation: 796

Ok, found the answer somewhere hidden in some forums.

After uploading, this will return the filename only(excluding the path):

$request->file('headerimg')->hashName();

Doc here hashname()

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163808

You could do something like this:

$saveTo = 'images';
$path = $request->file('headerimg')->store($saveTo);
$filename = substr($path, strlen($saveTo) + 1);

Upvotes: 1

Related Questions