This can be solve your problem if you don't mind file name
$destination = 'uploads/';
// ex: photo-5396e3816cc3d.jpg
$filename = Str::lower(
pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME)
.'-'
.uniqid()
.'.'
.$image->getClientOriginalExtension()
);
$image->move($destination, $filename);
$page->image = $filename;
$page->save();
Thanks, what's the difference between strtolower() and Str::lower()? Just laravel way of coding?
https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Str.php#L132 Str::lower() == mb_strtolower (== strtolower with multibyte string support)
barryvdh said:
https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Str.php#L132 Str::lower() == mb_strtolower (== strtolower with multibyte string support)
Thanks, barry. Maybe you know more convenient way of naming duplicate files? :)
If you save the page first, you can also place it in a folder with the same id, so you have uploads/123/image.jpg and uploads/124/image.jpg. If the filename isn't important, you can just use uniqid() . '.' .$image->getClientOriginalExtension()
and don't worry about the original filename.
If the file isn't in the public folder, you can save the original filename and the hashed filename, so you can return a file with the original filename, without worrying about duplicates (but more cpu intensive).
But the suggested solutions should work fine..
When i am using:
$image->getClientOriginalName();
It prints out this: filename.jpg
How can you use that:
$image->getClientOriginalExtension();
if above prints a filename plus file extension?
You can use pathinfo to get the name without extension.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community