Drupal duplicate filename throws AJAX error I am using the managed_file in a form to upload a file to Drupal. As you know Drupal saves the name of this file into its database and stores it in the path you give. 

Unfortunately, when a user uploads a file which has the filename of a previously existing file, he receives an AJAX error that arises from SQL. Even though Drupal manages to store this file with a different name in the folder, I can't prevent the AJAX error. 

Is there a solution for this?

Thanks.
As per Drupal structure you need to add option "FILE_EXISTS_RENAME" during save file for example :
 $file_temp = file_save_data($attachment->data, 'public:///' , FILE_EXISTS_RENAME);