--- filemanager_orig.module	2005-12-13 23:08:15.000000000 +0800
+++ filemanager.module	2005-12-13 23:12:41.000000000 +0800
@@ -670,11 +670,15 @@
     $file->directory = 0;
     $directories = db_query("SELECT directory, count(1) filecount FROM {file} WHERE private = '%s' GROUP BY directory ORDER BY directory ASC", $file->private);
 
+    // this while loop requires the $directories array to be ordered in ascending order
     while ($directory = db_fetch_object($directories)) {
-       // If the next directory in the list skips the current directory
-       // number use it or if the current directory has less than the file
-       // limit in it.
-      if ($directory > $file->directory || $directory->filecount < variable_get('filemanager_max_file_count', '2000')) {
+       // Only use the $directory if it has less files than the limit set
+       // or
+       // if the next directory from $directories skips a number (the directory in
+       // question does not exist), then the first term of the if-statement will 
+       // consider that directory as $file->directory only increments 
+       // by 1 on each iteration
+      if ($directory->directory > $file->directory || $directory->filecount < variable_get('filemanager_max_file_count', '2000')) {
         // If the directory is ok now lets make sure we don't already
         // have this filename in the directory.
         if (!file_exists(filemanager_create_path($file, FALSE)) && !file_exists(filemanager_create_path($file, TRUE))) {
