Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

Thanks, what's the difference between strtolower() and Str::lower()? Just laravel way of coding?

Last updated 2 years ago.
0

https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Str.php#L132 Str::lower() == mb_strtolower (== strtolower with multibyte string support)

Last updated 2 years ago.
0

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? :)

Last updated 2 years ago.
0

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..

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

You can use pathinfo to get the name without extension.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.