diff --git a/core/includes/file.inc b/core/includes/file.inc
index e1b9d97..021341b 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -5,6 +5,7 @@
  * API for handling file uploads and server file management.
  */
 
+use Drupal\Core\Utility\File;
 use Drupal\Core\StreamWrapper\LocalStream;
 use Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage;
 
@@ -1015,43 +1016,17 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
   return file_unmanaged_delete($path);
 }
 
-
-
 /**
  * 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.
- *
- * Compatibility: normal paths and stream wrappers.
- *
- * @param $filename
- *   The filename of the uploaded file.
- * @param $uri
- *   A string containing the destination URI of the file.
- *
- * @return
- *   TRUE on success, or FALSE on failure.
- *
  * @see move_uploaded_file()
  * @see http://drupal.org/node/515192
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::moveUploadedFile().
  */
 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;
+  return File::moveUploadedFile($filename, $uri);
 }
 
 /**
@@ -1242,359 +1217,103 @@ function file_get_mimetype($uri, $mapping = NULL) {
 /**
  * Sets the permissions on a file or directory.
  *
- * This function will use the system.file:chmod.directory and
- * system.file:chmod.file configuration for the default modes for directories
- * and uploaded/generated files. By default these will give everyone read access
- * so that users accessing the files with a user account without the webserver
- * group (e.g. via FTP) can read these files, and give group write permissions
- * so webserver group members (e.g. a vhost account) can alter files uploaded
- * and owned by the webserver.
- *
- * PHP's chmod does not support stream wrappers so we use our wrapper
- * implementation which interfaces with chmod() by default. Contrib wrappers
- * may override this behavior in their implementations as needed.
- *
- * @param $uri
- *   A string containing a URI file, or directory path.
- * @param $mode
- *   Integer value for the permissions. Consult PHP chmod() documentation for
- *   more information.
- *
- * @return bool
- *   TRUE for success, FALSE in the event of an error.
- *
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::chmod().
  */
 function drupal_chmod($uri, $mode = NULL) {
-  if (!isset($mode)) {
-    // Configuration system stores default modes as strings. We use octdec() so
-    // that the octal permission numbers can be expressed as integers or strings
-    // and will be converted correctly in both cases.
-    if (is_dir($uri)) {
-      $mode = octdec(config('system.file')->get('chmod.directory'));
-      if (!$mode) {
-        $mode = 0775;
-      }
-    }
-    else {
-      $mode = octdec(config('system.file')->get('chmod.file'));
-      if (!$mode) {
-        $mode = 0664;
-      }
-    }
-  }
-
-  // If this URI is a stream, pass it off to the appropriate stream wrapper.
-  // Otherwise, attempt PHP's chmod. This allows use of drupal_chmod even
-  // for unmanaged files outside of the stream wrapper interface.
-  if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
-    if ($wrapper->chmod($mode)) {
-      return TRUE;
-    }
-  }
-  else {
-    if (@chmod($uri, $mode)) {
-      return TRUE;
-    }
-  }
-
-  watchdog('file', 'The file permissions could not be set on %uri.', array('%uri' => $uri), WATCHDOG_ERROR);
-  return FALSE;
+  return File::chmod($uri, $mode);
 }
 
 /**
  * Deletes a file.
  *
- * PHP's unlink() is broken on Windows, as it can fail to remove a file
- * when it has a read-only flag set.
- *
- * @param $uri
- *   A URI or pathname.
- * @param $context
- *   Refer to http://php.net/manual/ref.stream.php
- *
- * @return
- *   Boolean TRUE on success, or FALSE on failure.
- *
  * @see unlink()
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::unlink().
  */
 function drupal_unlink($uri, $context = NULL) {
-  $scheme = file_uri_scheme($uri);
-  if (!file_stream_wrapper_valid_scheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
-    chmod($uri, 0600);
-  }
-  if ($context) {
-    return unlink($uri, $context);
-  }
-  else {
-    return unlink($uri);
-  }
+  return File::unlink($uri, $context);
 }
 
 /**
  * Returns the absolute local filesystem path of a stream URI.
  *
- * This function was originally written to ease the conversion of 6.x code to
- * use 7.x stream wrappers. However, it assumes that every URI may be resolved
- * to an absolute local filesystem path, and this assumption fails when stream
- * wrappers are used to support remote file storage. Remote stream wrappers
- * may implement the realpath method by always returning FALSE. The use of
- * drupal_realpath() is discouraged, and is slowly being removed from core
- * functions where possible.
- *
- * Only use this function if you know that the stream wrapper in the URI uses
- * the local file system, and you need to pass an absolute path to a function
- * that is incompatible with stream URIs.
- *
- * @param $uri
- *   A stream wrapper URI or a filesystem path, possibly including one or more
- *   symbolic links.
- *
- * @return
- *   The absolute local filesystem path (with no symbolic links), or FALSE on
- *   failure.
- *
- * @todo This function is deprecated, and should be removed wherever possible.
- *
  * @see Drupal\Core\StreamWrapper\StreamWrapperInterface::realpath()
  * @see http://php.net/manual/function.realpath.php
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::realpath().
  */
 function drupal_realpath($uri) {
-  // If this URI is a stream, pass it off to the appropriate stream wrapper.
-  // Otherwise, attempt PHP's realpath. This allows use of drupal_realpath even
-  // for unmanaged files outside of the stream wrapper interface.
-  if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
-    return $wrapper->realpath();
-  }
-
-  return realpath($uri);
+  return File::realpath($uri);
 }
 
 /**
  * Gets the name of the directory from a given path.
  *
- * PHP's dirname() does not properly pass streams, so this function fills
- * that gap. It is backwards compatible with normal paths and will use
- * PHP's dirname() as a fallback.
- *
- * Compatibility: normal paths and stream wrappers.
- *
- * @param $uri
- *   A URI or path.
- *
- * @return
- *   A string containing the directory name.
- *
  * @see dirname()
  * @see http://drupal.org/node/515192
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::dirname().
  */
 function drupal_dirname($uri) {
-  $scheme = file_uri_scheme($uri);
-
-  if (file_stream_wrapper_valid_scheme($scheme)) {
-    return file_stream_wrapper_get_instance_by_scheme($scheme)->dirname($uri);
-  }
-  else {
-    return dirname($uri);
-  }
+  return File::dirname($uri);
 }
 
 /**
  * Gets the filename from a given path.
  *
- * PHP's basename() does not properly support streams or filenames beginning
- * with a non-US-ASCII character.
- *
  * @see http://bugs.php.net/bug.php?id=37738
  * @see basename()
  *
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::basename().
  */
 function drupal_basename($uri, $suffix = NULL) {
-  $separators = '/';
-  if (DIRECTORY_SEPARATOR != '/') {
-    // For Windows OS add special separator.
-    $separators .= DIRECTORY_SEPARATOR;
-  }
-  // Remove right-most slashes when $uri points to directory.
-  $uri = rtrim($uri, $separators);
-  // Returns the trailing part of the $uri starting after one of the directory
-  // separators.
-  $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
-  // Cuts off a suffix from the filename.
-  if ($suffix) {
-    $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
-  }
-  return $filename;
+  return File::basename($uri, $suffix);
 }
 
 /**
  * Creates a directory, optionally creating missing components in the path to
  * the directory.
  *
- * When PHP's mkdir() creates a directory, the requested mode is affected by the
- * process's umask. This function overrides the umask and sets the mode
- * explicitly for all directory components created.
- *
- * @param $uri
- *   A URI or pathname.
- * @param $mode
- *   Mode given to created directories. Defaults to the directory mode
- *   configured in the Drupal installation. It must have a leading zero.
- * @param $recursive
- *   Create directories recursively, defaults to FALSE. Cannot work with a mode
- *   which denies writing or execution to the owner of the process.
- * @param $context
- *   Refer to http://php.net/manual/ref.stream.php
- *
- * @return
- *   Boolean TRUE on success, or FALSE on failure.
- *
  * @see mkdir()
  * @see http://drupal.org/node/515192
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::mkdir().
  */
 function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
-  if (!isset($mode)) {
-    // Configuration system stores default mode as strings.
-    $mode = FALSE;
-    // During early update there's no container.
-    if (is_object(Drupal::getContainer())) {
-      $mode = octdec(config('system.file')->get('chmod.directory'));
-    }
-    if (!$mode) {
-      $mode = 0775;
-    }
-  }
-
-  // If the URI has a scheme, don't override the umask - schemes can handle this
-  // issue in their own implementation.
-  if (file_uri_scheme($uri)) {
-    return _drupal_mkdir_call($uri, $mode, $recursive, $context);
-  }
-
-  // If recursive, create each missing component of the parent directory
-  // individually and set the mode explicitly to override the umask.
-  if ($recursive) {
-    // Ensure the path is using DIRECTORY_SEPARATOR.
-    $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri);
-    // Determine the components of the path.
-    $components = explode(DIRECTORY_SEPARATOR, $uri);
-    array_pop($components);
-    $recursive_path = '';
-    foreach ($components as $component) {
-      $recursive_path .= $component;
-
-      if (!file_exists($recursive_path)) {
-        if (!_drupal_mkdir_call($recursive_path, $mode, FALSE, $context)) {
-          return FALSE;
-        }
-        // Not necessary to use drupal_chmod() as there is no scheme.
-        if (!chmod($recursive_path, $mode)) {
-          return FALSE;
-        }
-      }
-
-      $recursive_path .= DIRECTORY_SEPARATOR;
-    }
-  }
-
-  // Do not check if the top-level directory already exists, as this condition
-  // must cause this function to fail.
-  if (!_drupal_mkdir_call($uri, $mode, FALSE, $context)) {
-    return FALSE;
-  }
-  // Not necessary to use drupal_chmod() as there is no scheme.
-  return chmod($uri, $mode);
-}
-
-/**
- * Helper function. Ensures we don't pass a NULL as a context resource to
- * mkdir().
- *
- * @see drupal_mkdir()
- */
-function _drupal_mkdir_call($uri, $mode, $recursive, $context) {
-  if (is_null($context)) {
-    return mkdir($uri, $mode, $recursive);
-  }
-  else {
-    return mkdir($uri, $mode, $recursive, $context);
-  }
+  return File::mkdir($uri, $mode, $recursive, $context);
 }
 
 /**
  * Removes a directory.
  *
- * PHP's rmdir() is broken on Windows, as it can fail to remove a directory
- * when it has a read-only flag set.
- *
- * @param $uri
- *   A URI or pathname.
- * @param $context
- *   Refer to http://php.net/manual/ref.stream.php
- *
- * @return
- *   Boolean TRUE on success, or FALSE on failure.
- *
  * @see rmdir()
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::rmdir().
  */
 function drupal_rmdir($uri, $context = NULL) {
-  $scheme = file_uri_scheme($uri);
-  if (!file_stream_wrapper_valid_scheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
-    chmod($uri, 0700);
-  }
-  if ($context) {
-    return rmdir($uri, $context);
-  }
-  else {
-    return rmdir($uri);
-  }
+  return File::rmdir($uri, $context);
 }
 
 /**
  * Creates a file with a unique filename in the specified directory.
  *
- * PHP's tempnam() does not return a URI like we want. This function
- * will return a URI if given a URI, or it will return a filepath if
- * given a filepath.
- *
- * Compatibility: normal paths and stream wrappers.
- *
- * @param $directory
- *   The directory where the temporary filename will be created.
- * @param $prefix
- *   The prefix of the generated temporary filename.
- *   Note: Windows uses only the first three characters of prefix.
- *
- * @return
- *   The new temporary filename, or FALSE on failure.
- *
  * @see tempnam()
  * @see http://drupal.org/node/515192
  * @ingroup php_wrappers
+ *
+ * @deprecated Use \Drupal\Core\Utility\File::tempnam().
  */
 function drupal_tempnam($directory, $prefix) {
-  $scheme = file_uri_scheme($directory);
-
-  if (file_stream_wrapper_valid_scheme($scheme)) {
-    $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
-
-    if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
-      return $scheme . '://' . drupal_basename($filename);
-    }
-    else {
-      return FALSE;
-    }
-  }
-  else {
-    // Handle as a normal tempnam() call.
-    return tempnam($directory, $prefix);
-  }
+  return File::tempnam($directory, $prefix);
 }
 
 /**
diff --git a/core/lib/Drupal/Core/Utility/File.php b/core/lib/Drupal/Core/Utility/File.php
new file mode 100644
index 0000000..a7333e0
--- /dev/null
+++ b/core/lib/Drupal/Core/Utility/File.php
@@ -0,0 +1,410 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Utility\File.
+ */
+
+namespace Drupal\Core\Utility;
+
+/**
+ * @todo.
+ */
+class File {
+
+  /**
+   * 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.
+   *
+   * Compatibility: normal paths and stream wrappers.
+   *
+   * @param string $filename
+   *   The filename of the uploaded file.
+   * @param string $uri
+   *   A string containing the destination URI of the file.
+   *
+   * @return bool
+   *   TRUE on success, or FALSE on failure.
+   *
+   * @see move_uploaded_file()
+   * @see http://drupal.org/node/515192
+   * @ingroup php_wrappers
+   */
+  public static function moveUploadedFile($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 = static::realpath($uri)) {
+        $result = move_uploaded_file($filename, $realpath);
+      }
+      else {
+        $result = move_uploaded_file($filename, $uri);
+      }
+    }
+
+    return $result;
+  }
+
+  /**
+   * Sets the permissions on a file or directory.
+   *
+   * This function will use the system.file:chmod.directory and
+   * system.file:chmod.file configuration for the default modes for directories
+   * and uploaded/generated files. By default these will give everyone read access
+   * so that users accessing the files with a user account without the webserver
+   * group (e.g. via FTP) can read these files, and give group write permissions
+   * so webserver group members (e.g. a vhost account) can alter files uploaded
+   * and owned by the webserver.
+   *
+   * PHP's chmod does not support stream wrappers so we use our wrapper
+   * implementation which interfaces with chmod() by default. Contrib wrappers
+   * may override this behavior in their implementations as needed.
+   *
+   * @param string $uri
+   *   A string containing a URI file, or directory path.
+   * @param int $mode
+   *   Integer value for the permissions. Consult PHP chmod() documentation for
+   *   more information.
+   *
+   * @return bool
+   *   TRUE for success, FALSE in the event of an error.
+   *
+   * @ingroup php_wrappers
+   */
+  public static function chmod($uri, $mode = NULL) {
+    if (!isset($mode)) {
+      // Configuration system stores default modes as strings. We use octdec() so
+      // that the octal permission numbers can be expressed as integers or strings
+      // and will be converted correctly in both cases.
+      if (is_dir($uri)) {
+        $mode = octdec(config('system.file')->get('chmod.directory'));
+        if (!$mode) {
+          $mode = 0775;
+        }
+      }
+      else {
+        $mode = octdec(config('system.file')->get('chmod.file'));
+        if (!$mode) {
+          $mode = 0664;
+        }
+      }
+    }
+
+    // If this URI is a stream, pass it off to the appropriate stream wrapper.
+    // Otherwise, attempt PHP's chmod. This allows use of drupal_chmod even
+    // for unmanaged files outside of the stream wrapper interface.
+    if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
+      if ($wrapper->chmod($mode)) {
+        return TRUE;
+      }
+    }
+    else {
+      if (@chmod($uri, $mode)) {
+        return TRUE;
+      }
+    }
+
+    watchdog('file', 'The file permissions could not be set on %uri.', array('%uri' => $uri), WATCHDOG_ERROR);
+    return FALSE;
+  }
+
+  /**
+   * Deletes a file.
+   *
+   * PHP's unlink() is broken on Windows, as it can fail to remove a file
+   * when it has a read-only flag set.
+   *
+   * @param string $uri
+   *   A URI or pathname.
+   * @param resource|null $context
+   *   Refer to http://php.net/manual/ref.stream.php
+   *
+   * @return bool
+   *   Boolean TRUE on success, or FALSE on failure.
+   *
+   * @see unlink()
+   * @ingroup php_wrappers
+   */
+  public static function unlink($uri, $context = NULL) {
+    $scheme = file_uri_scheme($uri);
+    if (!file_stream_wrapper_valid_scheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
+      chmod($uri, 0600);
+    }
+    if ($context) {
+      return unlink($uri, $context);
+    }
+    else {
+      return unlink($uri);
+    }
+  }
+
+  /**
+   * Returns the absolute local filesystem path of a stream URI.
+   *
+   * This function was originally written to ease the conversion of 6.x code to
+   * use 7.x stream wrappers. However, it assumes that every URI may be resolved
+   * to an absolute local filesystem path, and this assumption fails when stream
+   * wrappers are used to support remote file storage. Remote stream wrappers
+   * may implement the realpath method by always returning FALSE. The use of
+   * drupal_realpath() is discouraged, and is slowly being removed from core
+   * functions where possible.
+   *
+   * Only use this function if you know that the stream wrapper in the URI uses
+   * the local file system, and you need to pass an absolute path to a function
+   * that is incompatible with stream URIs.
+   *
+   * @param string $uri
+   *   A stream wrapper URI or a filesystem path, possibly including one or more
+   *   symbolic links.
+   *
+   * @return string|bool
+   *   The absolute local filesystem path (with no symbolic links), or FALSE on
+   *   failure.
+   *
+   * @todo This function is deprecated, and should be removed wherever possible.
+   *
+   * @see Drupal\Core\StreamWrapper\StreamWrapperInterface::realpath()
+   * @see http://php.net/manual/function.realpath.php
+   * @ingroup php_wrappers
+   */
+  public static function realpath($uri) {
+    // If this URI is a stream, pass it off to the appropriate stream wrapper.
+    // Otherwise, attempt PHP's realpath. This allows use of drupal_realpath even
+    // for unmanaged files outside of the stream wrapper interface.
+    if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
+      return $wrapper->realpath();
+    }
+
+    return realpath($uri);
+  }
+
+  /**
+   * Gets the name of the directory from a given path.
+   *
+   * PHP's dirname() does not properly pass streams, so this function fills
+   * that gap. It is backwards compatible with normal paths and will use
+   * PHP's dirname() as a fallback.
+   *
+   * Compatibility: normal paths and stream wrappers.
+   *
+   * @param string $uri
+   *   A URI or path.
+   *
+   * @return string
+   *   A string containing the directory name.
+   *
+   * @see dirname()
+   * @see http://drupal.org/node/515192
+   * @ingroup php_wrappers
+   */
+  public static function dirname($uri) {
+    $scheme = file_uri_scheme($uri);
+
+    if (file_stream_wrapper_valid_scheme($scheme)) {
+      return file_stream_wrapper_get_instance_by_scheme($scheme)->dirname($uri);
+    }
+    else {
+      return dirname($uri);
+    }
+  }
+
+  /**
+   * Gets the filename from a given path.
+   *
+   * PHP's basename() does not properly support streams or filenames beginning
+   * with a non-US-ASCII character.
+   *
+   * @see http://bugs.php.net/bug.php?id=37738
+   * @see basename()
+   *
+   * @ingroup php_wrappers
+   */
+  public static function basename($uri, $suffix = NULL) {
+    $separators = '/';
+    if (DIRECTORY_SEPARATOR != '/') {
+      // For Windows OS add special separator.
+      $separators .= DIRECTORY_SEPARATOR;
+    }
+    // Remove right-most slashes when $uri points to directory.
+    $uri = rtrim($uri, $separators);
+    // Returns the trailing part of the $uri starting after one of the directory
+    // separators.
+    $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
+    // Cuts off a suffix from the filename.
+    if ($suffix) {
+      $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
+    }
+    return $filename;
+  }
+
+  /**
+   * Creates a directory, optionally creating missing components in the path to
+   * the directory.
+   *
+   * When PHP's mkdir() creates a directory, the requested mode is affected by the
+   * process's umask. This function overrides the umask and sets the mode
+   * explicitly for all directory components created.
+   *
+   * @param string $uri
+   *   A URI or pathname.
+   * @param int $mode
+   *   Mode given to created directories. Defaults to the directory mode
+   *   configured in the Drupal installation. It must have a leading zero.
+   * @param bool $recursive
+   *   Create directories recursively, defaults to FALSE. Cannot work with a mode
+   *   which denies writing or execution to the owner of the process.
+   * @param resource|null $context
+   *   Refer to http://php.net/manual/ref.stream.php
+   *
+   * @return bool
+   *   Boolean TRUE on success, or FALSE on failure.
+   *
+   * @see mkdir()
+   * @see http://drupal.org/node/515192
+   * @ingroup php_wrappers
+   */
+  public static function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
+    if (!isset($mode)) {
+      // Configuration system stores default mode as strings.
+      $mode = FALSE;
+      // During early update there's no container.
+      if (is_object(\Drupal::getContainer())) {
+        $mode = octdec(config('system.file')->get('chmod.directory'));
+      }
+      if (!$mode) {
+        $mode = 0775;
+      }
+    }
+
+    // If the URI has a scheme, don't override the umask - schemes can handle this
+    // issue in their own implementation.
+    if (file_uri_scheme($uri)) {
+      return static::mkdirCall($uri, $mode, $recursive, $context);
+    }
+
+    // If recursive, create each missing component of the parent directory
+    // individually and set the mode explicitly to override the umask.
+    if ($recursive) {
+      // Ensure the path is using DIRECTORY_SEPARATOR.
+      $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri);
+      // Determine the components of the path.
+      $components = explode(DIRECTORY_SEPARATOR, $uri);
+      array_pop($components);
+      $recursive_path = '';
+      foreach ($components as $component) {
+        $recursive_path .= $component;
+
+        if (!file_exists($recursive_path)) {
+          if (!static::mkdirCall($recursive_path, $mode, FALSE, $context)) {
+            return FALSE;
+          }
+          // Not necessary to use drupal_chmod() as there is no scheme.
+          if (!chmod($recursive_path, $mode)) {
+            return FALSE;
+          }
+        }
+
+        $recursive_path .= DIRECTORY_SEPARATOR;
+      }
+    }
+
+    // Do not check if the top-level directory already exists, as this condition
+    // must cause this function to fail.
+    if (!static::mkdirCall($uri, $mode, FALSE, $context)) {
+      return FALSE;
+    }
+    // Not necessary to use drupal_chmod() as there is no scheme.
+    return chmod($uri, $mode);
+  }
+
+  /**
+   * Removes a directory.
+   *
+   * PHP's rmdir() is broken on Windows, as it can fail to remove a directory
+   * when it has a read-only flag set.
+   *
+   * @param string $uri
+   *   A URI or pathname.
+   * @param resource|null $context
+   *   Refer to http://php.net/manual/ref.stream.php
+   *
+   * @return bool
+   *   Boolean TRUE on success, or FALSE on failure.
+   *
+   * @see rmdir()
+   * @ingroup php_wrappers
+   */
+  public static function rmdir($uri, $context = NULL) {
+    $scheme = file_uri_scheme($uri);
+    if (!file_stream_wrapper_valid_scheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
+      chmod($uri, 0700);
+    }
+    if ($context) {
+      return rmdir($uri, $context);
+    }
+    else {
+      return rmdir($uri);
+    }
+  }
+
+  /**
+   * Creates a file with a unique filename in the specified directory.
+   *
+   * PHP's tempnam() does not return a URI like we want. This function
+   * will return a URI if given a URI, or it will return a filepath if
+   * given a filepath.
+   *
+   * Compatibility: normal paths and stream wrappers.
+   *
+   * @param string $directory
+   *   The directory where the temporary filename will be created.
+   * @param string $prefix
+   *   The prefix of the generated temporary filename.
+   *   Note: Windows uses only the first three characters of prefix.
+   *
+   * @return string|bool
+   *   The new temporary filename, or FALSE on failure.
+   *
+   * @see tempnam()
+   * @see http://drupal.org/node/515192
+   * @ingroup php_wrappers
+   */
+  public static function tempnam($directory, $prefix) {
+    $scheme = file_uri_scheme($directory);
+
+    if (file_stream_wrapper_valid_scheme($scheme)) {
+      $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
+
+      if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
+        return $scheme . '://' . static::basename($filename);
+      }
+      else {
+        return FALSE;
+      }
+    }
+    else {
+      // Handle as a normal tempnam() call.
+      return tempnam($directory, $prefix);
+    }
+  }
+
+  /**
+   * Helper function. Ensures we don't pass a NULL as a context resource to
+   * mkdir().
+   *
+   * @see drupal_mkdir()
+   */
+  protected static function mkdirCall($uri, $mode, $recursive, $context) {
+    if (is_null($context)) {
+      return mkdir($uri, $mode, $recursive);
+    }
+    else {
+      return mkdir($uri, $mode, $recursive, $context);
+    }
+  }
+
+}
