Index: imagefield.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/imagefield/imagefield.module,v
retrieving revision 1.9
diff -u -r1.9 imagefield.module
--- imagefield.module	2 Aug 2006 10:27:39 -0000	1.9
+++ imagefield.module	13 Aug 2006 01:11:30 -0000
@@ -109,7 +109,11 @@
  */
 function imagefield_file_insert($node, &$file, $field) {
   $fieldname = $field['field_name'];
-  if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
+
+	// get the file path - appending the content type specific folder to the 'files' dir if used
+	$file_path = file_directory_path() . ($field['widget']['image_path'] ? '/' . $field['widget']['image_path'] : '');
+
+	if ($file = file_save_upload((object)$file, $file_path . '/'.$file['filename'])) {
     $file = (array)$file;
     $file['fid'] = db_next_id('{files}_fid');
     db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
@@ -227,14 +231,48 @@
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+			$form['image_path'] = array(
+				'#type' => 'textfield', 
+				'#title' => t('Image path'), 
+				'#default_value' => $widget['image_path'] ? $widget['image_path'] : '', 
+				'#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => theme('placeholder', variable_get('file_directory_path', 'files')))), 
+				'#after_build' => array('imagefield_check_directory'),
+			);
       return $form;
     case 'validate':
       break;
     case 'save':
-      return array('max_resolution');
+      return array('max_resolution', 'image_path');
   }
 }
 
+/**
+ * Create the image directory relative to the 'files' dir - if user specified one
+ * Won't allow form submit unless the directory exists & is writable
+ * 
+ * TODO: find way to handle creating multiple directories - file_check_directory won't create sub-directories?
+ * 
+ * @param $element
+ *   The form element containing the name of the directory to check.
+ */ 
+function imagefield_check_directory($element) {
+	if(!empty($element['#value'])) {
+		$original_path = $element['#value']; // note the user input file path (relative to the files dir)
+		$element['#value'] = file_directory_path() . '/' . $element['#value']; // pre-pend the files path to the image directory
+		// create the directory if it doesn't exist
+		file_check_directory($element['#value'], FILE_CREATE_DIRECTORY);
+		// check the directory exists & is writable 
+		if (!is_dir($element['#value'])) {
+			form_set_error($element['#parents'][0], t('Could not create the directory %path. Please create the path yourself, or alter/remove the \'Image Path\' entry to create this widget.', array('%path' => theme('placeholder', $element['#value']))));
+		}
+		if (!is_writable($element['#value'])) {
+			form_set_error($element['#parents'][0], t('The directory %path is not writable. Please change the write permissions, or alter/remove the \'Image Path\' entry to create this widget.', array('%path' => theme('placeholder', $element['#value']))));
+		}
+		// re-assign the user input to the field value (so it does'nt show the 'full' file path - only the user input relative to the files dir)
+		$element['#value'] = $original_path;
+	}
+	return $element;
+}
 
 function _imagefield_scale_image($file, $resolution = 0) {
   $info = image_get_info($file['filepath']);
