Laravel generate a unique ID using Storage::put

Viewed 23023

I use Laravel Storage::putFile() when I store uploaded files and I like the convenience of it's automatic unique file name and how it takes care of file extension.

Now I want to get a file from a remote server (file_get_contents($url)) and store it like I did for uploaded files, but I don't find any equal method in the docs.

In https://laravel.com/docs/5.5/filesystem#storing-files in the put method, you have to specify the file name.

3 Answers
$filename = uniqid(). '.' .File::extension($file->getClientOriginalName());
//uniqid() is php function to generate uniqid but you can use time() etc.

$path = "Files/Images/"
Storage::disk('local')->put($path.$filename,file_get_contents($file));

To be sure the filename is unique and to get the extension of your file url, you can do like this :

$ext = pathinfo($url, PATHINFO_EXTENSION);    
$filename = bcrypt($url).time().'.'.$ext;
Related