--- upload.module.orig	2006-03-08 00:08:11.000000000 -0700
+++ upload.module	2006-03-07 23:58:52.000000000 -0700
@@ -77,7 +77,7 @@
     // Add handlers for previewing new uploads.
     if ($_SESSION['file_uploads']) {
       foreach ($_SESSION['file_uploads'] as $key => $file) {
-        $filename = file_create_filename($file->filename, file_create_path());
+        $filename = file_create_filename($file->filename, file_create_path(upload_get_path()));
         $items[] = array(
           'path' => $filename, 'title' => t('file download'),
           'callback' => 'upload_download',
@@ -99,6 +99,14 @@
     '#size' => 15, '#maxlength' => 10, '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
   );
 
+  $form['settings_general']['upload_user_store'] = array(
+		  '#type' => 'checkbox',
+		  '#title' => t('Store uploads in user\'s own subdirectory.'),
+		  '#return_value' => 1,
+		  '#default_value' => variable_get('upload_user_store', 0),
+		  '#description' => t('If checked, each user\'s uploads will be stored in the "%dir/user/$uid" subdirectory.', array('%dir' => variable_get('file_directory_path', 'files'))),
+	);
+
   $roles = user_roles(0, 'upload files');
 
   foreach ($roles as $rid => $role) {
@@ -117,6 +125,7 @@
     );
   }
 
+
   return $form;
 }
 
@@ -130,7 +139,7 @@
 
 function upload_file_download($file) {
   if (user_access('view uploaded files')) {
-    $file = file_create_path($file);
+    $file = file_create_path(upload_get_path().$file);
     $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
     if ($file = db_fetch_object($result)) {
       $node = node_load($file->nid);
@@ -282,7 +291,7 @@
         foreach ($node->files as $key => $file) {
           if ($file->list && !$node->remove[$key]) {
             $rows[] = array(
-              '<a href="'. check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path())))) .'">'. check_plain($file->description ? $file->description : $file->filename) .'</a>',
+              '<a href="'. check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path(upload_get_path()))))) .'">'. check_plain($file->description ? $file->description : $file->filename) .'</a>',
               format_size($file->filesize)
             );
             // We save the list of files still in preview for later
@@ -299,7 +308,7 @@
         // place inline images for example).
         if (!variable_get('clean_url', 0)) {
           foreach ($previews as $file) {
-            $old = file_create_filename($file->filename, file_create_path());
+            $old = file_create_filename($file->filename, file_create_path(upload_get_path()));
             $new = url($old);
             $node->body = str_replace($old, $new, $node->body);
             $node->teaser = str_replace($old, $new, $node->teaser);
@@ -383,7 +392,7 @@
     if ($file->source) {
       // Only add a file if it's not marked for removal
       if (!$node->remove[$key]) {
-        if ($file = file_save_upload($file, $file->filename)) {
+        if ($file = file_save_upload($file,  upload_get_subdir().$file->filename)) {
           $fid = db_next_id('{files}_fid');
           db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
           db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $fid, $node->vid, $node->list[$key], $node->description[$key]);
@@ -484,7 +493,7 @@
       if ($file->list || $node->list[$key]) {
         $list[] = $key;
       }
-      $description = "<small>". file_create_url(($file->fid ? $file->filepath : file_create_filename($file->filename, file_create_path()))) ."</small>";
+      $description = "<small>". file_create_url(($file->fid ? $file->filepath : file_create_filename($file->filename, file_create_path(upload_get_path())))) ."</small>";
       $form['current']['description'][$key] = array('#type' => 'textfield', '#default_value' => $file->description ? $file->description : $file->filename, '#maxlength' => 256, '#description' => $description );
       $form['current']['size'][$key] = array('#type' => 'markup', '#value' => format_size($file->filesize));
     }
@@ -579,3 +588,24 @@
   print drupal_to_js(array('status' => TRUE, 'data' => $output));
   exit;
 }
+
+function upload_get_subdir() {
+  $upload_subdir = '';
+  if (variable_get('upload_user_store', 0)) {
+    global $user;
+    $upload_subdir = 'user/'. $user->uid . '/';
+  }
+  return $upload_subdir;
+}
+
+function upload_get_path() {
+  $upload_path = '';
+  if (variable_get('upload_user_store', 0)) {
+    $upload_path = file_create_path(upload_get_subdir());
+    if (!file_check_directory($upload_path, FILE_CREATE_DIRECTORY)) {
+      // failed to create subdirectory, use main directory
+      $upload_path = '';
+    }
+  }
+  return $upload_path;
+}
\ No newline at end of file
