0

To upload a file I use

Storage::disk('spaces')->putFile('uploads', $request->file, 'public');

The file is saved successfully on digital ocean spaces. But I want to rename it to something like this user_1_some_random_string.jpg. And then save it.

How can I do it?

Noob Coder
  • 2,816
  • 8
  • 36
  • 61

3 Answers3

0

The move method may be used to rename or move an existing file to a new location: Storage::move('hodor/oldfile-name.jpg', 'hodor/newfile-name.jpg');

Also: If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:

$path = $request->file('avatar')->storeAs(
    'avatars', $request->user()->id
);

More: https://laravel.com/docs/5.6/filesystem

Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
0

try use rand()

$ext  = $request->file('file')->getClientOriginalExtension();
$name = rand(11111,99999).'.'.$ext;
Storage::disk('spaces')->putFile('uploads', $name, 'public');
Just L
  • 80
  • 1
  • 2
  • 15
0

This is pretty old but I found an answer for anyone still looking.

You need to use the method putFileAs, as far as I can see

  1. the first parameter is the bucket/location. I tested this and it will create a new folder if you use 'uploads/testz' it created the 'testz' in the uploads folder on spaces.

  2. the second parameter is the request file object, in my case $request->file('file')

  3. the third parameter is the filename that you WANT to store the file as. I tested and if you 'testz/<yourspecialfilename.extension>' it will create the same folder as in parameter 1, which suggests to me that the method concats param 1 and 3. So the full snippet in my controller is

     public function create(Request $request){ Storage::disk('spaces')->putFileAs('uploads/testz', $request->file('file'), 'mychosenfilename.mydesiredextension');
     return redirect()->back();}
    
Linton Caldecott
  • 449
  • 6
  • 17