Search code examples
laravellaravel-5

Laravel generate a unique ID using Storage::put


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.


Solution

  • $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));