--- ../../drupal-4.5.1/modules/upload.module	2004-10-20 12:57:50.000000000 -0400
+++ upload.module	2004-12-26 13:11:16.694199788 -0500
@@ -45,7 +45,7 @@ function upload_menu($may_cache) {
     // 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',
@@ -63,7 +63,8 @@ function upload_menu($may_cache) {
 function upload_admin() {
   system_settings_save();
 
-  $group .= form_textfield(t('Maximum total file size'), 'upload_maxsize_total', variable_get('upload_maxsize_total', 0), 5, 5, t('The maximum size of a file a user can upload in megabytes. Enter 0 for unlimited.'));
+  $group = form_textfield(t('Maximum total file size'), 'upload_maxsize_total', variable_get('upload_maxsize_total', 0), 5, 5, t('The maximum size of a file a user can upload in megabytes. Enter 0 for unlimited.'));
+  $group .= form_checkbox(t('Store uploads in user\'s own subdirectory.'), 'upload_user_store', 1, variable_get('upload_user_store', 0), t('If checked, each user\'s uploads will be stored in a subdirectory named for the user.  For example, a user named "joe" will upload files into the "%dir/joe/" subdirectory.', array('%dir' => variable_get('file_directory_path', 'files'))));
 
   $output = form_group(t('General settings'), $group);
 
@@ -217,7 +218,7 @@ function upload_nodeapi(&$node, $op, $ar
         foreach ($node->files as $file) {
           if ($file->list) {
             $rows[] = array(
-              '<a href="'. ($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))) . '">'. $file->filename .'</a>',
+              '<a href="'. ($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path(upload_get_path())))) . '">'. $file->filename .'</a>',
               format_size($file->filesize)
             );
             // We save the list of files still in preview for later
@@ -234,7 +235,7 @@ function upload_nodeapi(&$node, $op, $ar
         // 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);
@@ -280,7 +281,7 @@ function upload_save($node) {
       unset($_SESSION['file_uploads'][$file->source]);
 
       // Insert new files:
-      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, list) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)",
                  $fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $node->list[$key]);
@@ -317,7 +318,7 @@ function upload_form($node) {
       $rows[] = array(
         form_checkbox('', "remove][$key", 1, $file->remove),
         form_checkbox('', "list][$key", 1, $file->list),
-        $file->filename ."<br /><small>". file_create_url(($file->fid ? $file->filepath : file_create_filename($file->filename, file_create_path()))) ."</small>",
+        $file->filename ."<br /><small>". file_create_url(($file->fid ? $file->filepath : file_create_filename($file->filename, file_create_path(upload_get_path())))) ."</small>",
         format_size($file->filesize)
       );
     }
@@ -347,4 +348,25 @@ function upload_load($node) {
   return $files;
 }
 
+function upload_get_subdir() {
+  $upload_subdir = '';
+  if (variable_get('upload_user_store', 0)) {
+    global $user;
+    $upload_subdir = preg_replace('/[^0-9a-zA-Z-]/', '_', $user->name) . '/';
+  }
+  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;
+}
+
 ?>
