 core/includes/file.inc |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/core/includes/file.inc b/core/includes/file.inc
index babf465..d97471f 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -1583,8 +1583,13 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
 /**
  * Moves an uploaded file to a new location.
  *
- * PHP's move_uploaded_file() does not properly support streams if safe_mode
- * or open_basedir are enabled, so this function fills that gap.
+ * PHP's move_uploaded_file() does not properly support URIs if safe_mode or
+ * open_basedir are enabled, so this function fills that gap for local URIs.
+ *
+ * move_uploaded_file() is also very inefficient when supplied with URIs as it
+ * will perform a copy and then a delete. With this function, if the destination
+ * URI scheme supports the realpath() method, i.e. it is a local scheme, then
+ * the I/O produced will be negligible.
  *
  * Compatibility: normal paths and stream wrappers.
  * @see http://drupal.org/node/515192
@@ -1601,20 +1606,9 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
  * @ingroup php_wrappers
  */
 function drupal_move_uploaded_file($filename, $uri) {
-  $result = @move_uploaded_file($filename, $uri);
-  // PHP's move_uploaded_file() does not properly support streams if safe_mode
-  // or open_basedir are enabled so if the move failed, try finding a real path
-  // and retry the move operation.
-  if (!$result) {
-    if ($realpath = drupal_realpath($uri)) {
-      $result = move_uploaded_file($filename, $realpath);
-    }
-    else {
-      $result = move_uploaded_file($filename, $uri);
-    }
-  }
-
-  return $result;
+  // Resolve the destination URI if posible.
+  $destination = drupal_realpath($uri) ?: $uri;
+  return move_uploaded_file($filename, $destination);
 }
 
 /**
