19a20
>   $replace_mode = variable_get('upload_replace_replace_mode', FILE_EXISTS_RENAME);
60a62,63
>       $query_size = "UPDATE {files} SET filesize = %d WHERE fid = %d";
>       
63d65
<         //move the blocking file to a temparary location
65,67c67,79
<         if (!file_move($from, $tmp_destination)) {
<           drupal_set_message('The file %old could not be moved to %new', array('%old' => $desired_destination, '%new' => $tmp_destination), 'error');
<           return;
---
>         if ($replace_mode == FILE_EXISTS_REPLACE) {
>           // delete the old file, letting the new one replace it
>           file_delete($from);
>         }
>         else {
>           //move the blocking file to a temporary location
>           if (!file_move($from, $tmp_destination)) {
>             drupal_set_message('The file %old could not be moved to %new', array('%old' => $desired_destination, '%new' => $tmp_destination), 'error');
>             return;
>           }
>           $tmp_destination = $from; //since the file copy may have placed the file in a modified location update the tmp destination with the actual location
>           //move blocking file was successful, update the DB
>           db_query($query, $tmp_destination, $blocking_file->fid);
69,71d80
<         $tmp_destination = $from; //since the file copy may have placed the file in a modified location update the tmp destination with the actual location
<         //move blocking file was successful, update the DB
<         db_query($query, $tmp_destination, $blocking_file->fid);
74d82
< 
87,90c95,115
<         //move the older file from temp to the new _0 location
<         if (!file_move($tmp_destination, $old_destination)) {
<           drupal_set_message('The file %old could not be moved to %new', array('%old' => $tmp_destination, '%new' => $old_destination), 'error');
<           return;
---
>         if ($replace_mode == FILE_EXISTS_REPLACE) {
>           // we want to replace the old file, making sure that nodes that refer
>           // to the file that previously had the new file's filename do not
>           // point to the old copy (with _0...n appended)
>           // update files table to set entry for old file to point to new one
>           $newfile_location = $from;
>           db_query($query, $newfile_location, $blocking_file->fid);
>           db_query($query_size, filesize($newfile_location), $blocking_file->fid);
>           //set the blocking file's path
>           $blocking_file->filepath = $newfile_location;
>         }
>         else {
>           //move the older file from temp to the new _0 location
>           if (!file_move($tmp_destination, $old_destination)) {
>             drupal_set_message('The file %old could not be moved to %new', array('%old' => $tmp_destination, '%new' => $old_destination), 'error');
>             return;
>           }
>           //move blocking file was successful, update the DB with the actual location after file copy, so we use tmp_destination as it was updated during the move
>           db_query($query, $tmp_destination, $blocking_file->fid);
>           //set the blocking files newpath
>           $blocking_file->filepath = $tmp_destination;
92,95d116
<         //move blocking file was successful, update the DB with the actual location after file copy, so we use tmp_destination as it was updated during the move
<         db_query($query, $tmp_destination, $blocking_file->fid);
<         //set the blocking files newpath
<         $blocking_file->filepath = $tmp_destination;
151a173,213
> 
> /**
>  * Menu callback for settings
>  */
> function upload_replace_admin() {
> 
>   include_once('./includes/file.inc');
> 
>   $form = array();
> 
>   $form['upload_replace_replace_mode'] = array(
>     '#type' => 'radios',
>     '#title' => t('Replace mode'),
>     '#description' => t('This setting controls what happens if a file is uploaded and one by the same filename already exists.'),
>     '#default_value' => variable_get('upload_replace_replace_mode', FILE_EXISTS_RENAME),
>     '#options' => array(
>       FILE_EXISTS_RENAME => t('Rename the previous file appending an incrementing number to the filename (filename_0.ext ... filename_<em>n</em>.ext)'),
>       FILE_EXISTS_REPLACE => t('Replace the previously existing file and keep the filename')
>     ),
>   );
> 
>   return system_settings_form($form);
> }
> 
> /**
>  * Implementation of hook_menu()
>  */
> function upload_replace_menu() {
>   $items = array();
> 
>   $items['admin/settings/upload_replace'] = array(
>     'title' => 'Upload Replace',
>     'description' => 'Configure settings for Upload Replace.',
>     'page callback' => 'drupal_get_form',
>     'page arguments' => array('upload_replace_admin'),
>     'access arguments' => array('administer site configuration'),
>     'type' => MENU_NORMAL_ITEM,
>   );
> 
>   return $items;
> }
\ No newline at end of file
