Laravel : To rename an uploaded file automatically

Use this one

$file->move($destinationPath, $fileName);

For unique file Name saving

In 5.3 (best for me because use md5_file hashname in Illuminate\Http\UploadedFile):

public function saveFile(Request $request) {
    $file = $request->file('your_input_name')->store('your_path','your_disk');
}

In 5.4 (use not unique Str::random(40) hashname in Illuminate\Http\UploadedFile). I Use this code to ensure unique name:

public function saveFile(Request $request) {
    $md5Name = md5_file($request->file('your_input_name')->getRealPath());
    $guessExtension = $request->file('your_input_name')->guessExtension();
    $file = $request->file('your_input_name')->storeAs('your_path', $md5Name.'.'.$guessExtension  ,'your_disk');
}

Tags:

Laravel