1

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:

<?php function tsaudio_form_system_theme_settings_alter(&$form,&$form_state) {

$form['slideshow'] = array(
'#type' => 'fieldset',
'#title' => t('Background'),
'#collapsible' => TRUE,
'#collapsed' => 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' => 'textfield',
'#title' => 'Path to background image',
'#default_value' => $bg_path,
'#disabled' => FALSE,
);

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

$form['#submit'][] = 'tsaudio_settings_submit';}
function tsaudio_settings_submit($form, &$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->filename);
$destination = 'public://' . $parts['basename'];
$file->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;}

}?>

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] => 1 [:db_insert_placeholder_1] => 
ctrldailybg4.jpg [:db_insert_placeholder_2] => 
temporary://ctrldailybg4_1.jpg [:db_insert_placeholder_3] => 
image/jpeg [:db_insert_placeholder_4] => 1344028 
[:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => 
1436760809 ) in drupal_write_record() (line 7316 of /homepages
/9/d539887599/htdocs/towersignal_com/includes/common.inc).
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.