--- filefield_old.module	2007-05-04 15:59:49.000000000 -0400
+++ filefield.module	2007-05-04 17:05:52.000000000 -0400
@@ -93,7 +93,9 @@ function filefield_field_settings($op, $
  */
 function filefield_file_insert($node, &$file, $field) {
   $fieldname = $field['field_name'];
-  if ($file = file_save_upload((object)$file, file_directory_path() . '/'.$file['filename'])) {
+  $filepath = file_create_path($field['widget']['file_path']) . '/' . $file['filename'];
+  
+  if (filefield_check_directory($field['widget']['file_path']) && $file = file_save_upload((object)$file, $filepath)) {
     $file = (array)$file;
     $file['fid'] = db_next_id('{files}_fid');
     db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
@@ -215,14 +217,53 @@ function filefield_widget_settings($op, 
         '#maxlength' => 64, 
         '#description' => t('Extensions a user can upload to this field. Seperate extensions with a space and do not include the leading dot.')
       );
+      $form['file_path'] = array(
+        '#type' => 'textfield', 
+        '#title' => t('File path'), 
+        '#default_value' => $widget['file_path'] ? $widget['file_path'] : '', 
+        '#description' => t('Optional subdirectory within the "%dir" directory where files will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))), 
+        '#after_build' => array('filefield_form_check_directory'),
+      );
       return $form;
     case 'validate':
       break;
     case 'save':
-      return array('file_extensions');
+      return array('file_extensions', 'file_path');
   }
 }
 
+/**
+ * Wrapper function for filefield_check_directory that accepts a form element
+ * to validate - if user specified one. Won't allow form submit unless the
+ * directory exists & is writable
+ * 
+ * @param $form_element
+ *   The form element containing the name of the directory to check.
+ */
+function filefield_form_check_directory($form_element) {
+  if(!empty($form_element['#value'])) {
+    filefield_check_directory($form_element['#value'], $form_element);
+  }
+  return $form_element;
+}
+
+/**
+ * Create the file directory relative to the 'files' dir recursively for every
+ * directory in the path.
+ * 
+ * @param $directory
+ *   The directory path under files to check, such as 'photo/path/here'
+ * @param $form_element
+ *   A form element to throw an error on if the directory is not writable
+ */ 
+function filefield_check_directory($directory, $form_element = array()) {
+  foreach(explode('/', $directory) as $dir) {
+    $dirs[] = $dir;
+    $path = file_create_path(implode($dirs,'/'));
+    file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
+  }
+  return true;
+}
 
 function filefield_clear_session() {
   if (is_array($_SESSION['filefield']) && count($_SESSION['filefield'])) {
@@ -283,7 +324,7 @@ function filefield_widget($op, $node, $f
           return FALSE;
         }
 
-        $filepath = file_create_filename($file['filename'], file_create_path());
+        $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['file_path']));
         if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
           if (strpos($filepath, file_directory_path()) !== FALSE) {
             $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/');
@@ -375,7 +416,7 @@ function _filefield_widget_form($node, $
           '#value' => theme('filefield_icon', $file),
         );
 
-        $filepath = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path()) : $file['filepath'];      
+        $filepath = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['file_path'])) : $file['filepath'];      
 
         $description = file_create_url($filepath);      
         $description = "<small>". check_plain($description) ."</small>";
