--- uploadpath.module	2007-06-15 18:44:39.000000000 -0400
+++ uploadpath.module	2007-12-04 17:20:34.000000000 -0500
@@ -59,25 +59,36 @@
  */
 function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   switch ($op) {
-    case 'submit':
-      if (isset($node->files)) {
-        foreach ($node->files as $key => $file) {
-          if (0 === strpos($key, 'upload_')) {  // Only rewrite the name when adding the file, not when updating it
-            // Get the new, prefixed file name
-            $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node)) . $node->files[$key]['filename'];
+    case 'insert':
+    case 'update':
+      if (user_access('upload files')) {
+          if (isset($node->files)) {
+            foreach ($node->files as $key => $file) {
+              if (0 === strpos($key, 'upload_')) {  // Only rewrite the name when adding the file, not when updating it
+                // Get the new, prefixed file name
+           
+                $tokenized = token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node);
+                $tokenized = str_replace(array(' ', "\n", "\t"), '_', $tokenized);
+                $dest =  $tokenized . $file->filename;
 
-            // Create the directory if it doesn't exist yet.
-            $dirs = explode('/', dirname($file_name));
-            $directory = file_directory_path();
-            while (count($dirs)) {
-              $directory .= '/' . array_shift($dirs);
-              file_check_directory($directory, FILE_CREATE_DIRECTORY);
+                // Create the directory if it doesn't exist yet.
+                $dirs = explode('/', dirname($dest));
+                $directory = file_directory_path();
+                while (count($dirs)) {
+                  $directory .= '/' . array_shift($dirs);
+                  file_check_directory($directory, FILE_CREATE_DIRECTORY);
+                }
+
+                // move file on filesystem
+                if (file_move($file->filepath, $dest, FILE_EXISTS_REPLACE)) {
+                    $node->files[$key] = $file;
+                    // update existing file in database
+                    db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file->filepath, $file->fid);
+                }
+              }
             }
-            // Change where the file will be saved to the specified directory.
-            $node->files[$key]['filename'] = $file_name;
           }
-        }
       }
       break;
   }
-}
+}
\ No newline at end of file
