Index: comment_upload.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_upload/comment_upload.module,v
retrieving revision 1.6.2.11
diff -u -p -r1.6.2.11 comment_upload.module
--- comment_upload.module	13 May 2007 19:54:01 -0000	1.6.2.11
+++ comment_upload.module	29 Sep 2007 14:16:56 -0000
@@ -209,6 +209,11 @@ function _comment_upload_save_files($com
     return;
   }
 
+  if (module_exists('uploadpath')) {
+    comment_upload_change_upload_path($comment);
+  }
+
+
   foreach ($comment['files'] as $file) {
     $file = (object)$file;
 
@@ -339,3 +344,24 @@ function comment_upload_file_download($f
     }
   }
 }
+
+function comment_upload_change_upload_path(&$comment) {
+  if (isset($comment['files'])) {
+    foreach ($comment['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', '') . '/', 'comment', (object) $comment)) . $comment['files'][$key]['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);
+        }
+        // Change where the file will be saved to the specified directory.
+        $comment['files'][$key]['filename'] = $file_name;
+      }
+    }
+  }
+}
