Need help with my form with duplicate file and resetting form Need help with my form for uploading an image on my theme's settings. I have a form that uploads an image that can be used as a background image. I have two problems with my form: first is how to delete the uploaded file or reset the form and second is an error message appears when uploading the same file again after resetting the theme's settings.

This is my form code I'm using:

&lt;?php function tsaudio_form_system_theme_settings_alter(&amp;$form,&amp;$form_state) {

$form['slideshow'] = array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Background'),
'#collapsible' =&gt; TRUE,
'#collapsed' =&gt; TRUE,
);

$bg_path = theme_get_setting('bg_path');if (file_uri_scheme($bg_path) == 'public') {
$bg_path = file_uri_target($bg_path);
}

$form['slideshow']['bg_path'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; 'Path to background image',
'#default_value' =&gt; $bg_path,
'#disabled' =&gt; FALSE,
);

$form['slideshow']['bg_upload'] = array(
'#type' =&gt; 'file',
'#title' =&gt; 'Upload background image',
'#description' =&gt; 'Upload a new image for the background.',
);
$form['reset'] = array(
'#type' =&gt; 'markup',
'#markup' =&gt; '&lt;input type="reset" value="Reset All Values" class="form-submit"&gt;',);

$form['#submit'][] = 'tsaudio_settings_submit';}
function tsaudio_settings_submit($form, &amp;$form_state) {
$settings = array();
// Get the previous value
$previous = 'public://' . $form['slideshow']['bg_path']['#default_value'];

$file = file_save_upload('bg_upload');
if ($file) {
$parts = pathinfo($file-&gt;filename);
$destination = 'public://' . $parts['basename'];
$file-&gt;status = FILE_STATUS_PERMANENT;

if(file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
  $_POST['bg_path'] = $form_state['values']['bg_path'] = $destination;
  // If new file has a different name than the old one, delete the old
  if ($destination != $previous) {
    drupal_unlink($previous);
  }
}
} else {
// Avoid error when the form is submitted without specifying a new image
$_POST['bg_path'] = $form_state['values']['bg_path'] = $previous;}

}?&gt;


And this is the error message I get when uploading the same file again after resetting the theme's settings to default.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062    
Duplicate entry 'temporary://ctrldailybg4_1.jpg' for key 'uri': 
INSERT INTO {file_managed} (uid, filename, uri, filemime, filesize,
status, timestamp) VALUES (:db_insert_placeholder_0, 
:db_insert_placeholder_1, :db_insert_placeholder_2, 
:db_insert_placeholder_3, :db_insert_placeholder_4, 
:db_insert_placeholder_5, :db_insert_placeholder_6); Array ( 
[:db_insert_placeholder_0] =&gt; 1 [:db_insert_placeholder_1] =&gt; 
ctrldailybg4.jpg [:db_insert_placeholder_2] =&gt; 
temporary://ctrldailybg4_1.jpg [:db_insert_placeholder_3] =&gt; 
image/jpeg [:db_insert_placeholder_4] =&gt; 1344028 
[:db_insert_placeholder_5] =&gt; 0 [:db_insert_placeholder_6] =&gt; 
1436760809 ) in drupal_write_record() (line 7316 of /homepages
/9/d539887599/htdocs/towersignal_com/includes/common.inc).
As described in the docs file_save_upload
file_save_upload($form_field_name, $validators = array(), 
$destination = FALSE, $replace = FILE_EXISTS_RENAME)

the last parameter $replace

$replace: Replace behavior when the destination file already exists:
FILE_EXISTS_REPLACE: Replace the existing file.
FILE_EXISTS_RENAME: Append _{incrementing number} until the filename is unique.
FILE_EXISTS_ERROR: Do nothing and return FALSE.