--- uploadpath.module	Wed Aug 13 03:07:31 2008
+++ uploadpath.module.new	Wed Aug 13 03:09:54 2008
@@ -132,7 +132,8 @@ function uploadpath_admin_settings() {
  */
 function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   switch ($op) {
-    case 'submit':
+    case 'insert':
+    case 'update':
       if(!in_array($node->type, variable_get('uploadpath_excluded_node_types',array()))){
 	      if (isset($node->files)) {
 	        foreach ($node->files as $key => $file) {
@@ -156,14 +157,14 @@ function uploadpath_nodeapi(&$node, $op,
                     
                 if(variable_get('uploadpath_clean_filenames', false) && $node->title){
                   //convert filename into semantic name based on node title :-)
-                  if($node->files[$key]['description'] && strlen($node->files[$key]['description']) > 3){
-                    $filename_root = $node->files[$key]['description']; //base filename on file desc
+                  if($node->files[$key]->description && strlen($node->files[$key]->description) > 3){
+                    $filename_root = $node->files[$key]->description; //base filename on file desc
                   }else{
                     $filename_root = $node->title; //base filename on node title
                   }
                   //drupal_set_message($filename_root);
                   //get path info of file
-                  $file_info = pathinfo($node->files[$key]['filename']);
+                  $file_info = pathinfo($node->files[$key]->filename);
                   /*crop lowercase node title to 35 chars max
                     replace anything except numbers and letters with underscore
                     add 10 digit unique id and file extension*/
@@ -177,22 +178,30 @@ function uploadpath_nodeapi(&$node, $op,
                   $file_name = $file_path . $file_name;
                 }else{
                   // apply new, prefixed file name by token replacing the path pattern
-                  $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $node->files[$key]['filename'];                      
+                  $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node)) . $node->files[$key]->filename;
                 }
                 // SECURITY NOTE:
                 // Tokens include user supplied information and could provide an attack vector.
                 // The current method of creating directories prevents the use of .. or other malicious
                 // paths, but future developers should keep this in mind when modifying the following code	
 	            // Create the directory if it doesn't exist yet.
+	            $file_name = file_directory_path() . '/' . $file_name;
 	            $dirs = explode('/', dirname($file_name));
-	            $directory = file_directory_path();
+	            $directory = '';
 	            while (count($dirs)) {
-	              $directory .= '/' . array_shift($dirs);
+	              $directory .= array_shift($dirs);
 	              file_check_directory($directory, FILE_CREATE_DIRECTORY);
+	              $directory .= '/';
 	            }
-	            // Change where the file will be saved to the specified directory.
-	            $node->files[$key]['filename'] = $file_name;
-                //drupal_set_message('<pre>AFTER:'.print_r($node->files[$key],1).'</pre>');
+
+              // move file on filesystem
+              if (file_move($file->filepath, $file_name, FILE_EXISTS_REPLACE)) {
+	              // Change where the file will be saved to the specified directory.
+		            $node->files[$key]->filepath = $file_name;
+                // update existing file in database
+                db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file->filepath, $file->fid);
+              }	            
+              //drupal_set_message('<pre>AFTER:'.print_r($node->files[$key],1).'</pre>');
 	          }
 	        }
 	      }
