diff --git a/upload_replace.module b/upload_replace.module
index cd2b349..75818b0 100644
--- a/upload_replace.module
+++ b/upload_replace.module
@@ -1,4 +1,5 @@
 <?php
+// $Id: upload_replace.module,v 1.1.2.5 2009/12/30 21:03:28 markdrupal Exp $
 /**
  * @file
  * A module file for providing functionality to replace files on upload
@@ -148,3 +149,65 @@ function upload_replace_file_update(&$new_file) {
 function upload_replace_file_delete(&$file) {
   $file->filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $file->fid));
 }
+
+/**
+ * Implementation of hook_filefield_paths_process_file()
+ * In case user has filefield_paths module installed.
+ */
+function upload_replace_filefield_paths_process_file($new, &$file, $settings, &$node, &$update) {
+  // Copied from filefield_paths.module function filefield_paths_filefield_paths_process_file()
+  if ($new) {
+    // Process filename
+    $file['filename']['old'] = $file['field']['filename'];
+    if (($file['filename']['new'] = $settings['filename']['value']) != '') {
+      $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'node', $node, $settings['filename']);
+      $file['filename']['new'] = filefield_paths_process_string($file['filename']['new'], 'field', array(0 => $file['field']), $settings['filename']);
+    }
+    else {
+      $file['filename']['new'] = $file['field']['filename'];
+    }
+  
+    // Process filepath
+    $file['filepath']['old'] = $file['field']['filepath'];
+    $file['filepath']['new'] = filefield_paths_process_string(file_directory_path() . '/' . $settings['filepath']['value'] . '/' . $file['filename']['new'], 'node', $node, $settings['filepath']);
+    $file['filepath']['new'] = filefield_paths_process_string($file['filepath']['new'], 'field', array(0 => $file['field']), $settings['filepath']);
+
+    // Finalize files if necessary
+    if (dirname($file['filepath']['new']) != dirname($file['field']['filepath']) || $file['filename']['new'] != $file['field']['filename']) {
+      if (filefield_paths_file_move($file)) {
+
+        // Fix reference to old paths.
+        if (isset($node->body) || module_exists('content')) {
+          $file['filepath']['new'] = str_replace($file['filename']['old'], $file['filename']['new'], $file['filepath']['new']);
+          $file_directory_path = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC ? file_directory_path() : 'system/files';
+
+          // Regular expression to replace old file reference.
+          $pattern = array(
+            'regex' => str_replace('/', '\/', $file_directory_path) . '([^"]*?)' . str_replace('/', '\/', str_replace(file_directory_path(), '', $file['filepath']['old'])),
+            'regex_enc' => str_replace('/', '\/', drupal_urlencode($file_directory_path)) . '([^"]*?)' . str_replace('/', '\/', str_replace(drupal_urlencode(file_directory_path()), '', drupal_urlencode($file['filepath']['old']))),
+            'replace' => $file_directory_path . '$1' . str_replace(file_directory_path(), '', $file['filepath']['new']),
+          );
+
+          // Process regular expression.
+          _filefield_paths_replace_pattern($pattern, $node, $update);
+        }
+
+        // Store new filename in file Array
+        $file['field']['filename'] = $file['filename']['new'];
+      }
+    }
+  }
+  // End copied code from filefield_paths.module function filefield_paths_filefield_paths_process_file()
+  
+  // Send file object data to the basic upload_replace_file_update function.
+  if ($new) {
+    $file_obj = field_file_load($file['field']['fid']);
+    // Cast to object since core functions use objects.
+    $file_obj = (object)$file_obj;
+
+    // Set some values for the upcoming function from what's available.
+    $file_obj->filename = $file['field']['filename'];
+    $file_obj->filepath = $file['field']['filepath'];
+    upload_replace_file_update($file_obj);
+  }
+}
\ No newline at end of file
