Reputation: 796
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
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
Reputation: 163808
You could do something like this:
$saveTo = 'images';
$path = $request->file('headerimg')->store($saveTo);
$filename = substr($path, strlen($saveTo) + 1);
Upvotes: 1