--- C:/Documents and Settings/Boki/Desktop/head/old imagefield/imagefield.module	Mon Jun 04 22:53:59 2007
+++ C:/Documents and Settings/Boki/Desktop/head/patches/unique filenames/imagefield.module	Mon Jun 04 22:54:30 2007
@@ -390,6 +390,18 @@
 
     if ($valid_image) {
       $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);
+      
+      // The images in session need to have unique filenames, in order not to mix up the thumbs
+      $cnt = time();
+      $filename_parts = explode('.', $file['filename']);
+      
+      $filename = ucfirst(strtolower($filename_parts[0])) . '_' . $cnt . '.' . strtolower($filename_parts[1]);
+      while(file_exists($path . '/' . $filename) || _imagefield_in_session($filename)) {
+        $cnt++;
+        $filename = ucfirst(strtolower($filename_parts[0])) . '_' . $cnt . '.' . strtolower($filename_parts[1]);
+      }
+      
+      $file['filename'] = $filename;
 
       // Create the filepath for the image preview
       if (function_exists('token_replace')) {
@@ -445,6 +457,22 @@
       $items[] = $file;
     }
   }
+}
+
+// See if a filename exists in the session, check every field
+function _imagefield_in_session($filename) {
+  if(count($_SESSION['imagefield']) > 0) { 
+    foreach($_SESSION['imagefield'] as $field) {
+      if(count($field) > 0) {
+        foreach($field as $file) {
+          if($file['filename'] == $filename) {
+            return true;  
+          }
+        }
+      }
+    }
+  }
+  return false;
 }
 
 function _imagefield_widget_form($node, $field, &$items) {
