diff --git includes/bootstrap.inc includes/bootstrap.inc
index 0c85173..e22fade 100644
--- includes/bootstrap.inc
+++ includes/bootstrap.inc
@@ -665,8 +665,8 @@ function drupal_get_filename($type, $name, $filename = NULL) {
 
       if (drupal_function_exists('drupal_system_listing')) {
         $matches = drupal_system_listing($mask, $dir, 'name', 0);
-        if (!empty($matches[$name]->filepath)) {
-          $files[$type][$name] = $matches[$name]->filepath;
+        if (!empty($matches[$name]->uri)) {
+          $files[$type][$name] = $matches[$name]->uri;
         }
       }
     }
diff --git includes/common.inc includes/common.inc
index e6831aa..d5634db 100644
--- includes/common.inc
+++ includes/common.inc
@@ -2544,8 +2544,8 @@ function drupal_get_css($css = NULL) {
   }
 
   $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
-  $directory = file_directory_path();
-  $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
+  $directory = file_directory_path('public');
+  $is_writable = is_dir($directory) && is_writable($directory);
 
   // A dummy query-string is added to filenames, to gain control over
   // browser-caching. The string changes on every update or full cache
@@ -2584,7 +2584,7 @@ function drupal_get_css($css = NULL) {
       case 'file':
         // Depending on whether aggregation is desired, include the file.
         if (!$item['preprocess'] || !($is_writable && $preprocess_css)) {
-          $rendered_css[] = '<link type="text/css" rel="stylesheet" media="' . $item['media'] . '" href="' . base_path() . $item['data'] . $query_string . '" />';
+          $rendered_css[] = '<link type="text/css" rel="stylesheet" media="' . $item['media'] . '" href="' . file_external_url($item['data']) . $query_string . '" />';
         }
         else {
           $preprocess_items[$item['media']][] = $item;
@@ -2605,8 +2605,8 @@ function drupal_get_css($css = NULL) {
       // Prefix filename to prevent blocking by firewalls which reject files
       // starting with "ad*".
       $filename = 'css_' . md5(serialize($items) . $query_string) . '.css';
-      $preprocess_file = drupal_build_css_cache($items, $filename);
-      $rendered_css['preprocess'] .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
+      $preprocess_file = file_external_url(drupal_build_css_cache($items, $filename));
+      $rendered_css['preprocess'] .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . $preprocess_file . '" />' . "\n";
     }
   }
   // Enclose the inline CSS with the style tag if required.
@@ -2632,9 +2632,8 @@ function drupal_build_css_cache($css, $filename) {
   $data = '';
 
   // Create the css/ within the files folder.
-  $csspath = file_create_path('css');
-  file_check_directory($csspath, FILE_CREATE_DIRECTORY);
-
+  $csspath = 'public://css';
+  file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
   if (!file_exists($csspath . '/' . $filename)) {
     // Build aggregate CSS file.
     foreach ($css as $stylesheet) {
@@ -2776,7 +2775,7 @@ function _drupal_load_stylesheet($matches) {
  * Delete all cached CSS files.
  */
 function drupal_clear_css_cache() {
-  file_scan_directory(file_create_path('css'), '/.*/', array('callback' => 'file_unmanaged_delete'));
+  file_scan_directory('public://css', '/.*/', array('callback' => 'file_unmanaged_delete'));
 }
 
 /**
@@ -3018,7 +3017,7 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
   $files = array();
   $preprocess_js = (variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
   $directory = file_directory_path();
-  $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
+  $is_writable = is_dir($directory) && is_writable($directory);
 
   // A dummy query-string is added to filenames, to gain control over
   // browser-caching. The string changes on every update or full cache
@@ -3050,7 +3049,7 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
 
       case 'file':
         if (!$item['preprocess'] || !$is_writable || !$preprocess_js) {
-          $no_preprocess .= '<script type="text/javascript"' . ($item['defer'] ? ' defer="defer"' : '') . ' src="' . base_path() . $item['data'] . ($item['cache'] ? $query_string : '?' . REQUEST_TIME) . "\"></script>\n";
+          $no_preprocess .= '<script type="text/javascript"' . ($item['defer'] ? ' defer="defer"' : '') . ' src="' . file_external_url($item['data']) . ($item['cache'] ? $query_string : '?' . REQUEST_TIME) . "\"></script>\n";
         }
         else {
           $files[$item['data']] = $item;
@@ -3069,8 +3068,8 @@ function drupal_get_js($scope = 'header', $javascript = NULL) {
     // Prefix filename to prevent blocking by firewalls which reject files
     // starting with "ad*".
     $filename = 'js_' . md5(serialize($files) . $query_string) . '.js';
-    $preprocess_file = drupal_build_js_cache($files, $filename);
-    $preprocessed .= '<script type="text/javascript" src="' . base_path() . $preprocess_file . '"></script>' . "\n";
+    $preprocess_file = file_external_url(drupal_build_js_cache($files, $filename));
+    $preprocessed .= '<script type="text/javascript" src="' . $preprocess_file . '"></script>' . "\n";
   }
 
   // Keep the order of JS files consistent as some are preprocessed and others are not.
@@ -3341,8 +3340,8 @@ function drupal_build_js_cache($files, $filename) {
   $contents = '';
 
   // Create the js/ within the files folder.
-  $jspath = file_create_path('js');
-  file_check_directory($jspath, FILE_CREATE_DIRECTORY);
+  $jspath = 'public://js';
+  file_prepare_directory($jspath, FILE_CREATE_DIRECTORY);
 
   if (!file_exists($jspath . '/' . $filename)) {
     // Build aggregate JS file.
@@ -3364,7 +3363,7 @@ function drupal_build_js_cache($files, $filename) {
  * Delete all cached JS files.
  */
 function drupal_clear_js_cache() {
-  file_scan_directory(file_create_path('js'), '/.*/', array('callback' => 'file_unmanaged_delete'));
+  file_scan_directory('public://js', '/.*/', array('callback' => 'file_unmanaged_delete'));
   variable_set('javascript_parsed', array());
 }
 
diff --git includes/file.inc includes/file.inc
index fc568a8..d66695e 100644
--- includes/file.inc
+++ includes/file.inc
@@ -24,7 +24,7 @@ require_once DRUPAL_ROOT . '/includes/stream_wrappers.inc';
  * - filename - Name of the file with no path components. This may differ from
  *   the basename of the filepath if the file is renamed to avoid overwriting
  *   an existing file.
- * - filepath - Path of the file relative to Drupal root.
+ * - uri - URI of the file.
  * - filemime - The file's MIME type.
  * - filesize - The size of the file in bytes.
  * - status - A bitmapped field indicating the status of the file. The first 8
@@ -35,28 +35,12 @@ require_once DRUPAL_ROOT . '/includes/stream_wrappers.inc';
  */
 
 /**
- * Flag to indicate that the 'public' file download method is enabled.
- *
- * When using this method, files are available from a regular HTTP request,
- * which provides no additional access restrictions.
- */
-define('FILE_DOWNLOADS_PUBLIC', 1);
-
-/**
- * Flag to indicate that the 'private' file download method is enabled.
- *
- * When using this method, all file requests are served by Drupal, during which
- * access-control checking can be performed.
- */
-define('FILE_DOWNLOADS_PRIVATE', 2);
-
-/**
- * Flag used by file_check_directory() -- create directory if not present.
+ * Flag used by file_prepare_directory() -- create directory if not present.
  */
 define('FILE_CREATE_DIRECTORY', 1);
 
 /**
- * Flag used by file_check_directory() -- file permissions may be changed.
+ * Flag used by file_prepare_directory() -- file permissions may be changed.
  */
 define('FILE_MODIFY_PERMISSIONS', 2);
 
@@ -220,6 +204,8 @@ function file_uri_target($uri) {
  *
  * @param $uri
  *   String reference containing the URI to normalize.
+ * @return
+ *	 Returns a string containing the normalized URI.
  */
 function file_stream_wrapper_uri_normalize($uri) {
   $scheme = file_uri_scheme($uri);
@@ -299,202 +285,158 @@ function file_stream_wrapper_get_instance_by_scheme($scheme) {
 }
 
 /**
- * Create the download path to a file.
+ * Creates the web accessible URL to a stream.
  *
- * @param $path A string containing the path of the file to generate URL for.
- * @return A string containing a URL that can be used to download the file.
- */
-function file_create_url($path) {
-  // Strip file_directory_path from $path. We only include relative paths in
-  // URLs.
-  $path = file_directory_strip($path);
-  switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
-    case FILE_DOWNLOADS_PUBLIC:
-      return $GLOBALS['base_url'] . '/' . file_directory_path() . '/' . str_replace('\\', '/', $path);
-    case FILE_DOWNLOADS_PRIVATE:
-      return url('system/files/' . $path, array('absolute' => TRUE));
-  }
-}
-
-/**
- * Make sure the destination is a complete path and resides in the file system
- * directory, if it is not prepend the file system directory.
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
  *
- * @param $destination
- *   A string containing the path to verify. If this value is omitted, Drupal's
- *   'files' directory will be used.
+ * @param $uri
+ *   The URI to for which we need an external URL.
  * @return
- *   A string containing the path to file, with file system directory appended
- *   if necessary, or FALSE if the path is invalid (i.e. outside the configured
- *   'files' or temp directories).
+ *   Returns a string containing a URL that may be used to access the file.
+ *   If the provided string already contains a preceding 'http', nothing is done
+ *   and the same string is returned. If a Valid stream wrapper could not be found
+ *   to generate an external URL, then FALSE will be returned.
  */
-function file_create_path($destination = NULL) {
-  $file_path = file_directory_path();
-  if (is_null($destination)) {
-    return $file_path;
-  }
-  // file_check_location() checks whether the destination is inside the Drupal
-  // files directory.
-  if (file_check_location($destination, $file_path)) {
-    return $destination;
-  }
-  // Check if the destination is instead inside the Drupal temporary files
-  // directory.
-  elseif (file_check_location($destination, file_directory_temp())) {
-    return $destination;
-  }
-  // Not found, try again with prefixed directory path.
-  elseif (file_check_location($file_path . '/' . $destination, $file_path)) {
-    return $file_path . '/' . $destination;
-  }
-  // File not found.
-  return FALSE;
+function file_external_url($uri) {
+  $scheme = file_uri_scheme($uri);
+
+  // TODO: Implement CDN integration hook stuff here
+  // see http://drupal.org/node/499156
+
+  if (!$scheme) {
+    // If this is not a properly formatted stream return the URI with the base url prepended.
+    return $GLOBALS['base_url'] . '/' . $uri;
+  }
+  elseif ($scheme == 'http' || $scheme == 'https') {
+    // Check for http so that we don't have to implement getExternalUrl() for the http wrapper.
+    return $uri;
+  }
+  else {
+    // Attempt to return an external URL using the appropriate wrapper.
+    if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
+      return $wrapper->getExternalUrl();
+    }
+    else {
+      return FALSE;
+    }
+  }
 }
 
 /**
- * Check that the directory exists and is writable.
+ * Prepares a directory by making sure that it is writable.
+ *
+ * This function will make sure that the directory exists and is writable. It
+ * will also try to create the directories if allowed.
  *
  * Directories need to have execute permissions to be considered a directory by
  * FTP servers, etc.
  *
- * @param $directory
- *   A string containing the name of a directory path.
- * @param $mode
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
+ * @param &$directory
+ *   A string reference containing the name of a directory path. A trailing
+ *   slash will be trimmed.
+ * @param $options
  *   A bitmask to indicate if the directory should be created if it does
  *   not exist (FILE_CREATE_DIRECTORY) or made writable if it is read-only
  *   (FILE_MODIFY_PERMISSIONS).
- * @param $form_item
- *   An optional string containing the name of a form item that any errors will
- *   be attached to. This is useful for settings forms that require the user to
- *   specify a writable directory. If it can't be made to work, a form error
- *   will be set preventing them from saving the settings.
  * @return
- *   FALSE when directory not found, or TRUE when directory exists.
+ *   Returns TRUE if the directory exists and is writable. Otherwise, FALSE
+ *   is returned.
  */
-function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
-  $directory = rtrim($directory, '/\\');
+function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
+  if (!file_stream_wrapper_valid_scheme(file_uri_scheme($directory))) {
+    // Only trim if we're not dealing with a stream.
+    $directory = rtrim($directory, '/\\');
+  }
 
   // Check if directory exists.
   if (!is_dir($directory)) {
     // Let mkdir() recursively create directories and use the default directory
     // permissions.
-    if (($mode & FILE_CREATE_DIRECTORY) && @mkdir($directory, variable_get('file_chmod_directory', 0775), TRUE)) {
-      drupal_chmod($directory);
-    }
-    else {
-      if ($form_item) {
-        form_set_error($form_item, t('The directory %directory does not exist.', array('%directory' => $directory)));
-        watchdog('file system', 'The directory %directory does not exist.', array('%directory' => $directory), WATCHDOG_ERROR);
-      }
-      return FALSE;
+    if (($options & FILE_CREATE_DIRECTORY) && @drupal_mkdir($directory, NULL, TRUE)) {
+      return drupal_chmod($directory);
     }
+    return FALSE;
   }
-
-  // Check to see if the directory is writable.
-  if (!is_writable($directory)) {
-    // If not able to modify permissions, or if able to, but chmod
-    // fails, return false.
-    if (!$mode || (($mode & FILE_MODIFY_PERMISSIONS) && !drupal_chmod($directory))) {
-      if ($form_item) {
-        form_set_error($form_item, t('The directory %directory is not writable', array('%directory' => $directory)));
-        watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array('%directory' => $directory), WATCHDOG_ERROR);
-      }
-      return FALSE;
-    }
+  // The directory exists, so check to see if it is writable.
+  $writable = is_writable($directory);
+  if (!$writable && ($options & FILE_MODIFY_PERMISSIONS)) {
+    return drupal_chmod($directory);
   }
 
-  if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("$directory/.htaccess")) {
-    $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
-    if (file_put_contents("$directory/.htaccess", $htaccess_lines)) {
-      drupal_chmod("$directory/.htaccess");
-    }
-    else {
-      $variables = array('%directory' => $directory, '!htaccess' => '<br />' . nl2br(check_plain($htaccess_lines)));
-      form_set_error($form_item, t("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables));
-      watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
-    }
-  }
-
-  return TRUE;
+  return $writable;
 }
 
 /**
- * Checks path to see if it is a directory, or a directory/file.
- *
- * @param $path
- *   A string containing a file path. This will be set to the directory's path.
- * @return
- *   If the directory is not in a Drupal writable directory, FALSE is returned.
- *   Otherwise, the base name of the path is returned.
+ * If missing, create a htaccess file in each Drupal files directory.
  */
-function file_check_path(&$path) {
-  // Check if path is a directory.
-  if (file_check_directory($path)) {
-    return '';
-  }
-
-  // Check if path is a possible dir/file.
-  $filename = basename($path);
-  $path = dirname($path);
-  if (file_check_directory($path)) {
-    return $filename;
-  }
-
-  return FALSE;
+function file_insure_htaccess() {
+  file_create_htaccess('public://', FALSE);
+  file_create_htaccess('private://', TRUE);
+  file_create_htaccess('temporary://', TRUE);
 }
 
 /**
- * Check if a file is really located inside $directory.
- *
- * This should be used to make sure a file specified is really located within
- * the directory to prevent exploits. Note that the file or path being checked
- * does not actually need to exist yet.
- *
- * @code
- *   // Returns FALSE:
- *   file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files');
- * @endcode
+ * Creates an htaccess file in the given directory.
  *
- * @param $source
- *   A string set to the file to check.
  * @param $directory
- *   A string where the file should be located.
- * @return
- *   FALSE if the path does not exist in the directory; otherwise, the real
- *   path of the source.
+ *   The directory.
+ * @param $private
+ *   FALSE indicates that $directory should be an open and public directory.
+ *   The default is TRUE which indicates a private and protected directory.
  */
-function file_check_location($source, $directory = '') {
-  $check = realpath($source);
-  if ($check) {
-    $source = $check;
+function file_create_htaccess($directory, $private = TRUE) {
+  if (file_uri_scheme($directory)) {
+    $directory = file_stream_wrapper_uri_normalize($directory);
   }
   else {
-    // This file does not yet exist.
-    $source = realpath(dirname($source)) . '/' . basename($source);
+    $directory = rtrim($directory, '/\\');
   }
-  $directory = realpath($directory);
-  if ($directory && strpos($source, $directory) !== 0) {
-    return FALSE;
+  $htaccess_path =  $directory . '/.htaccess';
+
+  if (file_exists($htaccess_path)) {
+    // Short circuit if the .htaccess file already exists.
+    return;
+  }
+
+  if ($private) {
+    // Private htaccess file.
+    $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nDeny from all\nOptions None\nOptions +FollowSymLinks";
+  }
+  else {
+    // Public htaccess file.
+    $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
+  }
+
+  // Write the .htaccess file.
+  if (file_put_contents($htaccess_path, $htaccess_lines)) {
+    drupal_chmod($htaccess_path);
+  }
+  else {
+    $variables = array('%directory' => $directory, '!htaccess' => '<br />' . nl2br(check_plain($htaccess_lines)));
+    watchdog('security', "Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>!htaccess</code>", $variables, WATCHDOG_ERROR);
   }
-  return $source;
 }
 
 /**
- * Load file objects from the database.
+ * Loads multiple file objects from the database.
  *
- * @param $fids
+ * @param array $fids
  *   An array of file IDs.
- * @param $conditions
- *   An array of conditions to match against the {files} table. These
+ * @param array $conditions
+ *   An array of conditions to match against the {file} table. These
  *   should be supplied in the form array('field_name' => 'field_value').
  * @return
- *  An array of file objects, indexed by fid.
+ *   Returns an array of file objects, indexed by fid.
  *
  * @see hook_file_load()
  * @see file_load()
  */
 function file_load_multiple($fids = array(), $conditions = array()) {
-  $query = db_select('files', 'f')->fields('f');
+  $query = db_select('file', 'f')->fields('f');
 
   // If the $fids array is populated, add those to the query.
   if ($fids) {
@@ -521,7 +463,7 @@ function file_load_multiple($fids = array(), $conditions = array()) {
 }
 
 /**
- * Load a file object from the database.
+ * Loads a file object from the database.
  *
  * @param $fid
  *  A file ID.
@@ -537,12 +479,12 @@ function file_load($fid) {
 }
 
 /**
- * Save a file object to the database.
+ * Saves a file object to the database.
  *
  * If the $file->fid is not set a new record will be added. Re-saving an
  * existing file will not change its status.
  *
- * @param $file
+ * @param object $file
  *   A file object returned by file_load().
  * @return
  *   The updated file object.
@@ -553,15 +495,15 @@ function file_load($fid) {
 function file_save($file) {
   $file = (object)$file;
   $file->timestamp = REQUEST_TIME;
-  $file->filesize = filesize($file->filepath);
+  $file->filesize = filesize($file->uri);
 
   if (empty($file->fid)) {
-    drupal_write_record('files', $file);
+    drupal_write_record('file', $file);
     // Inform modules about the newly added file.
     module_invoke_all('file_insert', $file);
   }
   else {
-    drupal_write_record('files', $file, 'fid');
+    drupal_write_record('file', $file, 'fid');
     // Inform modules that the file has been updated.
     module_invoke_all('file_update', $file);
   }
@@ -587,7 +529,9 @@ function file_save($file) {
  * @param $source
  *   A file object.
  * @param $destination
- *   A string containing the destination that $source should be copied to. This
+ *   A string containing the destination that $source should be copied to. This should
+ *   be a stream wrapper URI. If this value is omitted, Drupal's public files directory
+ *   will be used [public://].
  *   can be a complete file path, a directory path or, if this value is omitted,
  *   Drupal's 'files' directory will be used.
  * @param $replace
@@ -607,14 +551,14 @@ function file_save($file) {
 function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
   $source = (object)$source;
 
-  if ($filepath = file_unmanaged_copy($source->filepath, $destination, $replace)) {
+  if ($uri = file_unmanaged_copy($source->uri, $destination, $replace)) {
     $file = clone $source;
     $file->fid = NULL;
-    $file->filepath = $filepath;
-    $file->filename = basename($filepath);
+    $file->uri = $uri;
+    $file->filename = basename($uri);
     // If we are replacing an existing file re-use its database record.
     if ($replace == FILE_EXISTS_REPLACE) {
-      $existing_files = file_load_multiple(array(), array('filepath' => $filepath));
+      $existing_files = file_load_multiple(array(), array('uri' => $uri));
       if (count($existing_files)) {
         $existing = reset($existing_files);
         $file->fid = $existing->fid;
@@ -623,7 +567,7 @@ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
     }
     // If we are renaming around an existing file (rather than a directory),
     // use its basename for the filename.
-    else if ($replace == FILE_EXISTS_RENAME && is_file(file_create_path($destination))) {
+    else if ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
       $file->filename = basename($destination);
     }
 
@@ -634,11 +578,12 @@ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
 
     return $file;
   }
+
   return FALSE;
 }
 
 /**
- * Copy a file to a new location without calling any hooks or making any
+ * Copies a file to a new location without calling any hooks or making any
  * changes to the database.
  *
  * This is a powerful function that in many ways performs like an advanced
@@ -650,11 +595,10 @@ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
  *   replace the file or rename the file based on the $replace parameter.
  *
  * @param $source
- *   A string specifying the file location of the original file.
+ *   A string specifying the file location of the original file, or a URI.
  * @param $destination
- *   A string containing the destination that $source should be copied to. This
- *   can be a complete file path, a directory path or, if this value is omitted,
- *   Drupal's 'files' directory will be used.
+ *   A URI containing the destination that $source should be copied to. If
+ *   NULL the default scheme will be used as the destination.
  * @param $replace
  *   Replace behavior when the destination file already exists:
  *   - FILE_EXISTS_REPLACE - Replace the existing file.
@@ -670,38 +614,54 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
   $original_source = $source;
   $original_destination = $destination;
 
-  $source = realpath($source);
+  // Assert that the source file actually exists.
+  $source = drupal_realpath($source);
   if (!file_exists($source)) {
+    // TODO: Replace drupal_set_message() calls with exceptions instead.
     drupal_set_message(t('The specified file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $original_source)), 'error');
     return FALSE;
   }
 
-  $proposed_destination = file_create_path($destination);
-  $directory = $proposed_destination;
-  $basename = file_check_path($directory);
+  // Build a destination URI if necessary.
+  if (!isset($destination)) {
+    $destination = file_build_uri(basename($source));
+  }
 
-  // Make sure we at least have a valid directory.
-  if ($basename === FALSE) {
-    drupal_set_message(t('The specified file %file could not be copied, because the destination %directory is not properly configured. This is often caused by a problem with file or directory permissions.', array('%file' => $original_source, '%directory' => empty($original_destination) ? $proposed_destination : $original_destination)), 'error');
-    return FALSE;
+  // Assert that the destination contains a valid stream.
+  $destination_scheme = file_uri_scheme($destination);
+  if (!$destination_scheme || !file_stream_wrapper_valid_scheme($destination_scheme)) {
+    drupal_set_message(t('The specified file %file could not be copied, because the destination %destination is invalid. This is often caused by improper use of file_unmanaged_copy() or a missing stream wrapper.', array('%file' => $original_source, '%destination' => $destination)), 'error');
   }
 
-  // If the destination file is not specified then use the filename of the
-  // source file.
-  $basename = $basename ? $basename : basename($source);
-  $destination = file_destination($directory . '/' . $basename, $replace);
+  // Prepare the destination directory.
+  if (file_prepare_directory($destination)) {
+    // The destination is already a directory, so append the source basename.
+    $destination = file_stream_wrapper_uri_normalize($destination . '/' . basename($source));
+  }
+  else {
+    // Perhaps $destination is a dir/file?
+    $dirname = drupal_dirname($destination);
+    if (!file_prepare_directory($dirname)) {
+      // The destination is not valid.
+      drupal_set_message(t('The specified file %file could not be copied, because the destination %directory is not properly configured. This is often caused by a problem with file or directory permissions.', array('%file' => $original_source, '%directory' => $destination)), 'error');
+      return FALSE;
+    }
+  }
 
+  // Determine whether or not we can perform this operation based on overwrite rules.
+  $destination = file_destination($destination, $replace);
   if ($destination === FALSE) {
-    drupal_set_message(t('The file %file could not be copied because a file by that name already exists in the destination directory (%directory)', array('%file' => $source, '%directory' => $proposed_destination)), 'error');
+    drupal_set_message(t('The file %file could not be copied because a file by that name already exists in the destination directory (%directory)', array('%file' => $source, '%directory' => $destination)), 'error');
     return FALSE;
   }
-  // Make sure source and destination filenames are not the same, makes no
-  // sense to copy it if they are. In fact copying the file will most likely
-  // result in a 0 byte file. Which is bad. Real bad.
-  if ($source == realpath($destination)) {
+  
+  // Assert that the source and destination filenames are not the same.
+  if (drupal_realpath($source) == drupal_realpath($destination)) {
     drupal_set_message(t('The specified file %file was not copied because it would overwrite itself.', array('%file' => $source)), 'error');
     return FALSE;
   }
+  
+  // Perform the copy operation.
   if (!@copy($source, $destination)) {
     drupal_set_message(t('The specified file %file could not be copied.', array('%file' => $source)), 'error');
     return FALSE;
@@ -714,9 +674,21 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
 }
 
 /**
+ * Given a relative path, construct a uri into Drupal's default files location.
+ */
+function file_build_uri($path) {
+  $uri = variable_get('file_scheme_default', 'public') . '://' . $path;
+  return file_stream_wrapper_uri_normalize($uri);
+}
+
+
+/**
  * Determines the destination path for a file depending on how replacement of
  * existing files should be handled.
  *
+ * Compatibility: only stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $destination
  *   A string specifying the desired path.
  * @param $replace
@@ -726,8 +698,8 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
  *       unique.
  *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
  * @return
- *   The destination file path or FALSE if the file already exists and
- *   FILE_EXISTS_ERROR was specified.
+ *   Returns the destination file path, or FALSE if the file already exists
+ *   and FILE_EXISTS_ERROR is speciefied.
  */
 function file_destination($destination, $replace) {
   if (file_exists($destination)) {
@@ -738,7 +710,7 @@ function file_destination($destination, $replace) {
 
       case FILE_EXISTS_RENAME:
         $basename = basename($destination);
-        $directory = dirname($destination);
+        $directory = drupal_dirname($destination);
         $destination = file_create_filename($basename, $directory);
         break;
 
@@ -751,7 +723,7 @@ function file_destination($destination, $replace) {
 }
 
 /**
- * Move a file to a new location and update the file's database entry.
+ * Moves a file to a new location and update the file's database entry.
  *
  * Moving a file is performed by copying the file to the new location and then
  * deleting the original.
@@ -761,6 +733,9 @@ function file_destination($destination, $replace) {
  *   replace the file or rename the file based on the $replace parameter.
  * - Adds the new file to the files database.
  *
+ * Compatibility: only stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $source
  *   A file object.
  * @param $destination
@@ -778,7 +753,8 @@ function file_destination($destination, $replace) {
  *       unique.
  *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
  * @return
- *   Resulting file object for success, or FALSE in the event of an error.
+ *   Returns the resulting file object for success, or FALSE in the event of an
+ *   error.
  *
  * @see file_unmanaged_move()
  * @see hook_file_move()
@@ -786,14 +762,14 @@ function file_destination($destination, $replace) {
 function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
   $source = (object)$source;
 
-  if ($filepath = file_unmanaged_move($source->filepath, $destination, $replace)) {
+  if ($uri = file_unmanaged_move($source->uri, $destination, $replace)) {
     $delete_source = FALSE;
 
     $file = clone $source;
-    $file->filepath = $filepath;
+    $file->uri = $uri;
     // If we are replacing an existing file re-use its database record.
     if ($replace == FILE_EXISTS_REPLACE) {
-      $existing_files = file_load_multiple(array(), array('filepath' => $filepath));
+      $existing_files = file_load_multiple(array(), array('uri' => $uri));
       if (count($existing_files)) {
         $existing = reset($existing_files);
         $delete_source = TRUE;
@@ -802,7 +778,7 @@ function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
     }
     // If we are renaming around an existing file (rather than a directory),
     // use its basename for the filename.
-    else if ($replace == FILE_EXISTS_RENAME && is_file(file_create_path($destination))) {
+    else if ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
       $file->filename = basename($destination);
     }
 
@@ -822,9 +798,12 @@ function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
 }
 
 /**
- * Move a file to a new location without calling any hooks or making any
+ * Moves a file to a new location without calling any hooks or making any
  * changes to the database.
  *
+ * Compatibility: only stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $source
  *   A string specifying the file location of the original file.
  * @param $destination
@@ -838,7 +817,7 @@ function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
  *       unique.
  *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
  * @return
- *   The filepath of the moved file, or FALSE in the event of an error.
+ *   Returns the file path of the moved file, or FALSE in the event of an error.
  *
  * @see file_move()
  */
@@ -851,9 +830,9 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
 }
 
 /**
- * Munge the filename as needed for security purposes.
+ * Munges the filename as needed for security purposes.
  *
- * For instance the file name "exploit.php.pps" would become "exploit.php_.pps".
+ * For example, the file name "exploit.php.pps" would become "exploit.php_.pps".
  *
  * @param $filename
  *   The name of a file to modify.
@@ -862,7 +841,7 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
  * @param $alerts
  *   Whether alerts (watchdog, drupal_set_message()) should be displayed.
  * @return
- *   $filename The potentially modified $filename.
+ *   Returns the potentially modified file name.
  */
 function file_munge_filename($filename, $extensions, $alerts = TRUE) {
   $original = $filename;
@@ -897,33 +876,44 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) {
 }
 
 /**
- * Undo the effect of upload_munge_filename().
+ * Reverses the effect of file_munge_filename().
  *
  * @param $filename
  *   String with the filename to be unmunged.
  * @return
- *   An unmunged filename string.
+ *   Returns the unmunged file name.
  */
 function file_unmunge_filename($filename) {
   return str_replace('_.', '.', $filename);
 }
 
 /**
- * Create a full file path from a directory and filename.
+ * Creates a full filepath from a directory and filename.
  *
  * If a file with the specified name already exists, an alternative will be
  * used.
  *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $basename
- *   String filename
+ *   String containing the filename.
  * @param $directory
- *   String directory
+ *   String containing the directory.
  * @return
- *   File path consisting of $directory and a unique filename based off
- *   of $basename.
+ *   Returns the filepath consisting of $directory and a unique filename
+ *   based off of $basename.
  */
 function file_create_filename($basename, $directory) {
-  $destination = $directory . '/' . $basename;
+  // Are we dealing with a stream?
+  if (file_stream_wrapper_valid_scheme($directory)) {
+    $separator = '';
+  }
+  else {
+    $separator = '/';
+  }
+
+  $destination = $directory . $separator . $basename;
 
   if (file_exists($destination)) {
     // Destination file already exists, generate an alternative.
@@ -939,7 +929,7 @@ function file_create_filename($basename, $directory) {
 
     $counter = 0;
     do {
-      $destination = $directory . '/' . $name . '_' . $counter++ . $ext;
+      $destination = $directory . $separator . $name . '_' . $counter++ . $ext;
     } while (file_exists($destination));
   }
 
@@ -947,7 +937,7 @@ function file_create_filename($basename, $directory) {
 }
 
 /**
- * Delete a file and its database record.
+ * Deletes a file and its database record.
  *
  * If the $force parameter is not TRUE hook_file_references() will be called
  * to determine if the file is being used by any modules. If the file is being
@@ -958,8 +948,8 @@ function file_create_filename($basename, $directory) {
  * @param $force
  *   Boolean indicating that the file should be deleted even if
  *   hook_file_references() reports that the file is in use.
- * @return mixed
- *   TRUE for success, FALSE in the event of an error, or an array if the file
+ * @return
+ *   Returns TRUE for success, FALSE in the event of an error, or an array if the file
  *   is being used by another module. The array keys are the module's name and
  *   the values are the number of references.
  *
@@ -982,25 +972,28 @@ function file_delete($file, $force = FALSE) {
 
   // Make sure the file is deleted before removing its row from the
   // database, so UIs can still find the file in the database.
-  if (file_unmanaged_delete($file->filepath)) {
-    db_delete('files')->condition('fid', $file->fid)->execute();
+  if (file_unmanaged_delete($file->uri)) {
+    db_delete('file')->condition('fid', $file->fid)->execute();
     return TRUE;
   }
   return FALSE;
 }
 
 /**
- * Delete a file without calling any hooks or making any changes to the
+ * Deletes a file without calling any hooks or making any changes to the
  * database.
  *
  * This function should be used when the file to be deleted does not have an
  * entry recorded in the files table.
  *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $path
  *   A string containing a file path.
  * @return
- *   TRUE for success or path does not exist, or FALSE in the event of an
- *   error.
+ *   Returns TRUE for success or the path does not exists, or FALSE in the
+ *   event of an error.
  *
  * @see file_delete()
  * @see file_unmanaged_delete_recursive()
@@ -1026,7 +1019,7 @@ function file_unmanaged_delete($path) {
 }
 
 /**
- * Recursively delete all files and directories in the specified filepath.
+ * Recursively deletes all files and directories in the specified filepath.
  *
  * If the specified path is a directory then the function will call itself
  * recursively to process the contents. Once the contents have been removed the
@@ -1037,11 +1030,14 @@ function file_unmanaged_delete($path) {
  *
  * Note that this only deletes visible files with write permission.
  *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $path
  *   A string containing a file or directory path.
  * @return
- *   TRUE for success or path does not exist, or FALSE in the event of an
- *   error.
+ *   Returns TRUE for success or path does not exist, or FALSE in the event
+ *   of an error.
  *
  * @see file_unmanaged_delete()
  */
@@ -1062,7 +1058,7 @@ function file_unmanaged_delete_recursive($path) {
 }
 
 /**
- * Determine total disk space used by a single user or the whole filesystem.
+ * Determines total disk space used by a single user or the whole filesystem.
  *
  * @param $uid
  *   Optional. A user id, specifying NULL returns the total space used by all
@@ -1071,10 +1067,10 @@ function file_unmanaged_delete_recursive($path) {
  *   Optional. File Status to return. Combine with a bitwise OR(|) to return
  *   multiple statuses. The default status is FILE_STATUS_PERMANENT.
  * @return
- *   An integer containing the number of bytes used.
+ *   Returns an integer containing the number of bytes used.
  */
 function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
-  $query = db_select('files', 'f');
+  $query = db_select('file', 'f');
   // Use separate placeholders for the status to avoid a bug in some versions
   // of PHP. @see http://drupal.org/node/352956
   $query->where('f.status & :status1 = :status2', array(':status1' => $status, ':status2' => $status));
@@ -1088,9 +1084,13 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
 /**
  * Saves a file upload to a new location.
  *
- * The file will be added to the files table as a temporary file. Temporary
- * files are periodically cleaned. To make the file a permanent file call
- * assign the status and use file_save() to save it.
+ * The file will be added to the {file} table as a temporary file. Temporary
+ * files are periodically cleaned. To make the file a permanent file, assign
+ * the status and use file_save() to save the changes.
+ *
+ * Compatibility: source may be a normal path or stream, but the destination must
+ * always be a stream.
+ * @see http://drupal.org/node/515192
  *
  * @param $source
  *   A string specifying the name of the upload field to save.
@@ -1098,8 +1098,10 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
  *   An optional, associative array of callback functions used to validate the
  *   file. See file_validate() for a full discussion of the array format.
  * @param $destination
- *   A string containing the directory $source should be copied to. If this is
- *   not provided or is not writable, the temporary directory will be used.
+ *   A string containing the location $source should be copied to within the temp directory.
+ *   $destination can be a directory or directory and filename within the temp directory.
+ *   If not provided, the temporary directory will be used, and the $replace flag will
+ *   will be honored.
  * @param $replace
  *   Replace behavior when the destination file already exists:
  *   - FILE_EXISTS_REPLACE: Replace the existing file.
@@ -1107,8 +1109,8 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
  *     unique.
  *   - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  * @return
- *   An object containing the file information if the upload succeeded, FALSE
- *   in the event of an error, or NULL if no file was uploaded.
+ *   Returns an object containing the file information if the upload succeeded,
+ *   or FALSE in the event of an error, or NULL if no file was upload.
  */
 function file_save_upload($source, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
   global $user;
@@ -1165,25 +1167,24 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
   $file->uid      = $user->uid;
   $file->status   = 0;
   $file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions);
-  $file->filepath = $_FILES['files']['tmp_name'][$source];
+  $file->uri      = $_FILES['files']['tmp_name'][$source];
   $file->filemime = file_get_mimetype($file->filename);
   $file->filesize = $_FILES['files']['size'][$source];
 
   // Rename potentially executable files, to help prevent exploits.
   if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
     $file->filemime = 'text/plain';
-    $file->filepath .= '.txt';
+    $file->uri .= '.txt';
     $file->filename .= '.txt';
   }
 
-  // If the destination is not provided, or is not writable, then use the
-  // temporary directory.
-  if (empty($destination) || file_check_path($destination) === FALSE) {
-    $destination = file_directory_temp();
+  // If the destination is not provided, use the temporary directory.
+  if (empty($destination)) {
+    $destination = file_directory_path('temporary');
   }
 
   $file->source = $source;
-  $file->destination = file_destination(file_create_path($destination . '/' . $file->filename), $replace);
+  $file->destination = file_destination($destination . $file->filename, $replace);
   // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and
   // there's an existing file so we need to bail.
   if ($file->destination === FALSE) {
@@ -1213,19 +1214,19 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
   // Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary
   // directory. This overcomes open_basedir restrictions for future file
   // operations.
-  $file->filepath = $file->destination;
-  if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath)) {
+  $file->uri = $file->destination;
+  if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->uri)) {
     form_set_error($source, t('File upload error. Could not move uploaded file.'));
-    watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->filepath));
+    watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
     return FALSE;
   }
 
   // Set the permissions on the new file.
-  drupal_chmod($file->filepath);
+  drupal_chmod($file->uri);
 
   // If we are replacing an existing file re-use its database record.
   if ($replace == FILE_EXISTS_REPLACE) {
-    $existing_files = file_load_multiple(array(), array('filepath' => $file->filepath));
+    $existing_files = file_load_multiple(array(), array('uri' => $file->uri));
     if (count($existing_files)) {
       $existing = reset($existing_files);
       $file->fid = $existing->fid;
@@ -1241,9 +1242,8 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
   return FALSE;
 }
 
-
 /**
- * Check that a file meets the criteria specified by the validators.
+ * Checks that a file meets the criteria specified by the validators.
  *
  * After executing the validator callbacks specified hook_file_validate() will
  * also be called to allow other modules to report errors about the file.
@@ -1258,7 +1258,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
  *   indicates that the file passed validation. The functions will be called in
  *   the order specified.
  * @return
- *   An array contaning validation error messages.
+ *   Returns an array containing validation error messages.
  *
  * @see hook_file_validate()
  */
@@ -1277,12 +1277,12 @@ function file_validate(&$file, $validators = array()) {
 }
 
 /**
- * Check for files with names longer than we can store in the database.
+ * Asserts that a filename is short enough to store in the database.
  *
  * @param $file
  *   A Drupal file object.
  * @return
- *   An array. If the file name is too long, it will contain an error message.
+ *   Returns an array containing an error message if the filename is too long.
  */
 function file_validate_name_length($file) {
   $errors = array();
@@ -1290,22 +1290,22 @@ function file_validate_name_length($file) {
   if (empty($file->filename)) {
     $errors[] = t("The file's name is empty. Please give a name to the file.");
   }
-  if (strlen($file->filename) > 255) {
-    $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
+  if (strlen($file->filename) > 240) {
+    $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
   }
   return $errors;
 }
 
 /**
- * Check that the filename ends with an allowed extension.
+ * Asserts that the filename ends with an allowed extension.
  *
  * @param $file
  *   A Drupal file object.
  * @param $extensions
  *   A string with a space separated list of allowed extensions.
  * @return
- *   An array. If the file extension is not allowed, it will contain an error
- *   message.
+ *   Returns an array containing an error message if the file extension
+ *   is not allowed.
  *
  * @see hook_file_validate()
  */
@@ -1320,7 +1320,7 @@ function file_validate_extensions($file, $extensions) {
 }
 
 /**
- * Check that the file's size is below certain limits.
+ * Asserts that the file's size is below certain limits.
  *
  * This check is not enforced for the user #1.
  *
@@ -1333,8 +1333,8 @@ function file_validate_extensions($file, $extensions) {
  *   An integer specifying the maximum number of bytes the user is allowed.
  *   Zero indicates that no limit should be enforced.
  * @return
- *   An array. If the file size exceeds limits, it will contain an error
- *   message.
+ *   Returns an array containing an error message if the file size exceeds
+ *   limits.
  *
  * @see hook_file_validate()
  */
@@ -1358,19 +1358,19 @@ function file_validate_size($file, $file_limit = 0, $user_limit = 0) {
 }
 
 /**
- * Check that the file is recognized by image_get_info() as an image.
+ * Checks that the file is recognized by image_get_info() as an image.
  *
  * @param $file
  *   A Drupal file object.
  * @return
- *   An array. If the file is not an image, it will contain an error message.
+ *   Returns an array containing an error message if the file is not an image.
  *
  * @see hook_file_validate()
  */
 function file_validate_is_image($file) {
   $errors = array();
 
-  $info = image_get_info($file->filepath);
+  $info = image_get_info($file->uri);
   if (!$info || empty($info['extension'])) {
     $errors[] = t('Only JPEG, PNG and GIF images are allowed.');
   }
@@ -1379,10 +1379,9 @@ function file_validate_is_image($file) {
 }
 
 /**
- * If the file is an image verify that its dimensions are within the specified
- * maximum and minimum dimensions.
+ * Asserts that the dimensions of an image are within the specified parameters.
  *
- * Non-image files will be ignored. If a image toolkit is available the image
+ * Non-image files will be ignored. If an image toolkit is available the image
  * will be scalled to fit within the desired maximum dimensions.
  *
  * @param $file
@@ -1397,8 +1396,8 @@ function file_validate_is_image($file) {
  *   An optional string in the form WIDTHxHEIGHT. This will check that the
  *   image meets a minimum size. A value of 0 indicates no restriction.
  * @return
- *   An array. If the file is an image and did not meet the requirements, it
- *   will contain an error message.
+ *   Returns an array containing an error message if the file is an image and
+ *   exceeded the specified paramaters.
  *
  * @see hook_file_validate()
  */
@@ -1406,13 +1405,13 @@ function file_validate_image_resolution($file, $maximum_dimensions = 0, $minimum
   $errors = array();
 
   // Check first that the file is an image.
-  if ($info = image_get_info($file->filepath)) {
+  if ($info = image_get_info($file->uri)) {
     if ($maximum_dimensions) {
       // Check that it is smaller than the given dimensions.
       list($width, $height) = explode('x', $maximum_dimensions);
       if ($info['width'] > $width || $info['height'] > $height) {
         // Try to resize the image to fit the dimensions.
-        if ($image = image_load($file->filepath)) {
+        if ($image = image_load($file->uri)) {
           image_scale($image, $width, $height);
           image_save($image);
           $file->filesize = $image->info['file_size'];
@@ -1437,7 +1436,10 @@ function file_validate_image_resolution($file, $maximum_dimensions = 0, $minimum
 }
 
 /**
- * Save a string to the specified destination and create a database file entry.
+ * Saves a string to the specified destination and creates a database file entry.
+ *
+ * Compatibility: only stream wrappers.
+ * @see http://drupal.org/node/515192
  *
  * @param $data
  *   A string containing the contents of the file.
@@ -1454,25 +1456,25 @@ function file_validate_image_resolution($file, $maximum_dimensions = 0, $minimum
  *       unique.
  *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
  * @return
- *   A file object, or FALSE on error.
+ *   Returns a file object, or FALSE on error.
  *
  * @see file_unmanaged_save_data()
  */
 function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
   global $user;
 
-  if ($filepath = file_unmanaged_save_data($data, $destination, $replace)) {
+  if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
     // Create a file object.
     $file = new stdClass();
     $file->fid = NULL;
-    $file->filepath = $filepath;
-    $file->filename = basename($filepath);
-    $file->filemime = file_get_mimetype($file->filepath);
+    $file->uri = $uri;
+    $file->filename = basename($uri);
+    $file->filemime = file_get_mimetype($file->uri);
     $file->uid      = $user->uid;
     $file->status  |= FILE_STATUS_PERMANENT;
     // If we are replacing an existing file re-use its database record.
     if ($replace == FILE_EXISTS_REPLACE) {
-      $existing_files = file_load_multiple(array(), array('filepath' => $filepath));
+      $existing_files = file_load_multiple(array(), array('uri' => $uri));
       if (count($existing_files)) {
         $existing = reset($existing_files);
         $file->fid = $existing->fid;
@@ -1481,7 +1483,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
     }
     // If we are renaming around an existing file (rather than a directory),
     // use its basename for the filename.
-    else if ($replace == FILE_EXISTS_RENAME && is_file(file_create_path($destination))) {
+    else if ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
       $file->filename = basename($destination);
     }
 
@@ -1491,11 +1493,11 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
 }
 
 /**
- * Save a string to the specified destination without calling any hooks or
+ * Saves a string to the specified destination without calling any hooks or
  * making any changes to the database.
  *
  * This function is identical to file_save_data() except the file will not be
- * saved to the files table and none of the file_* hooks will be called.
+ * saved to the {file} table and none of the file_* hooks will be called.
  *
  * @param $data
  *   A string containing the contents of the file.
@@ -1510,13 +1512,13 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
  *                          unique.
  *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
  * @return
- *   A string with the path of the resulting file, or FALSE on error.
+ *   Returns a string with the path of the resulting file, or FALSE on error.
  *
  * @see file_save_data()
  */
 function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
   // Write the data to a temporary file.
-  $temp_name = tempnam(file_directory_temp(), 'file');
+  $temp_name = drupal_tempnam('temporary://', 'file');
   if (file_put_contents($temp_name, $data) === FALSE) {
     drupal_set_message(t('The file could not be created.'), 'error');
     return FALSE;
@@ -1527,15 +1529,14 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
 }
 
 /**
- * Transfer file using HTTP to client. Pipes a file through Drupal to the
- * client.
+ * Pipes a file through Drupal to the client using HTTP.
  *
- * @param $source
- *   String specifying the file path to transfer.
- * @param $headers
- *   An array of HTTP headers to send along with file.
+ * @param $uri
+ *   String specifying the file URI to transfer.
+ * @param
+ *   Returns an array of HTTP headers to send along with the file.
  */
-function file_transfer($source, $headers) {
+function file_transfer($uri, $headers) {
   if (ob_get_level()) {
     ob_end_clean();
   }
@@ -1544,11 +1545,9 @@ function file_transfer($source, $headers) {
     drupal_set_header($name, $value);
   }
   drupal_send_headers();
-
-  $source = file_create_path($source);
-
+  $scheme = file_uri_scheme($uri);
   // Transfer file in 1024 byte chunks to save memory usage.
-  if ($fd = fopen($source, 'rb')) {
+  if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
     while (!feof($fd)) {
       print fread($fd, 1024);
     }
@@ -1561,7 +1560,7 @@ function file_transfer($source, $headers) {
 }
 
 /**
- * Menu handler for private file transfers.
+ * Menu handler for private file transfers [private://].
  *
  * Call modules that implement hook_file_download() to find out if a file is
  * accessible and what headers it should be transferred with. If a module
@@ -1569,26 +1568,25 @@ function file_transfer($source, $headers) {
  * returned headers the download will start with the returned headers. If no
  * modules respond drupal_not_found() will be returned.
  *
+ * @see http://drupal.org/node/515192
+ *
  * @see hook_file_download()
  */
 function file_download() {
   // Merge remainder of arguments from GET['q'], into relative file path.
   $args = func_get_args();
-  $filepath = implode('/', $args);
-
-  // Maintain compatibility with old ?file=paths saved in node bodies.
-  if (isset($_GET['file'])) {
-    $filepath =  $_GET['file'];
-  }
+  $scheme = array_shift($args);
+  $target = implode('/', $args);
+  $uri = $scheme . '://' . $target;
 
-  if (file_exists(file_create_path($filepath))) {
+  if (file_exists($uri)) {
     // Let other modules provide headers and controls access to the file.
-    $headers = module_invoke_all('file_download', $filepath);
+    $headers = module_invoke_all('file_download', $uri);
     if (in_array(-1, $headers)) {
       return drupal_access_denied();
     }
     if (count($headers)) {
-      file_transfer($filepath, $headers);
+      file_transfer($uri, $headers);
     }
   }
   return drupal_not_found();
@@ -1602,6 +1600,9 @@ function file_download() {
  * prevents hidden files and directories (such as SVN working directories)
  * from being scanned.
  *
+ * Compatibility: only stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
  * @param $dir
  *   The base directory for the scan, without trailing slash.
  * @param $mask
@@ -1628,8 +1629,8 @@ function file_download() {
  *   Current depth of recursion. This parameter is only used internally and
  *   should not be passed.
  * @return
- *   An associative array (keyed on the provided key) of objects with
- *   'filepath', 'filename', and 'name' members corresponding to the
+ *   Returns an associative array (keyed on the provided key) of objects with
+ *   'uri', 'filename', and 'name' members corresponding to the
  *   matching files.
  */
 function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
@@ -1638,32 +1639,33 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
     'nomask' => '/(\.\.?|CVS)$/',
     'callback' => 0,
     'recurse' => TRUE,
-    'key' => 'filepath',
+    'key' => 'uri',
     'min_depth' => 0,
   );
 
-  $options['key'] = in_array($options['key'], array('filepath', 'filename', 'name')) ? $options['key'] : 'filepath';
+  $options['key'] = in_array($options['key'], array('uri', 'filename', 'name')) ? $options['key'] : 'uri';
   $files = array();
   if (is_dir($dir) && $handle = opendir($dir)) {
     while (FALSE !== ($filename = readdir($handle))) {
       if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
-        $filepath = "$dir/$filename";
-        if (is_dir($filepath) && $options['recurse']) {
+        $uri = "$dir/$filename";
+        $uri = file_stream_wrapper_uri_normalize($uri);
+        if (is_dir($uri) && $options['recurse']) {
           // Give priority to files in this folder by merging them in after any subdirectory files.
-          $files = array_merge(file_scan_directory($filepath, $mask, $options, $depth + 1), $files);
+          $files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
         }
         elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
           // Always use this match over anything already set in $files with the
           // same $$options['key'].
           $file = (object) array(
-            'filepath' => $filepath,
+            'uri' => $uri,
             'filename' => $filename,
             'name' => pathinfo($filename, PATHINFO_FILENAME),
           );
           $key = $options['key'];
           $files[$file->$key] = $file;
           if ($options['callback']) {
-            $options['callback']($filepath);
+            $options['callback']($uri);
           }
         }
       }
@@ -1676,78 +1678,37 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
 }
 
 /**
- * Determine the default temporary directory.
+ * Determines the local directory path of a given wrapper.
  *
- * @return
- *   A string containing a temp directory.
- */
-function file_directory_temp() {
-  $temporary_directory = variable_get('file_directory_temp');
-
-  if (is_null($temporary_directory)) {
-    $directories = array();
-
-    // Has PHP been set with an upload_tmp_dir?
-    if (ini_get('upload_tmp_dir')) {
-      $directories[] = ini_get('upload_tmp_dir');
-    }
-
-    // Operating system specific dirs.
-    if (substr(PHP_OS, 0, 3) == 'WIN') {
-      $directories[] = 'c:/windows/temp';
-      $directories[] = 'c:/winnt/temp';
-    }
-    else {
-      $directories[] = '/tmp';
-    }
-
-    foreach ($directories as $directory) {
-      if (!$temporary_directory && is_dir($directory)) {
-        $temporary_directory = $directory;
-      }
-    }
-
-    // if a directory has been found, use it, otherwise default to 'files/tmp'
-    $temporary_directory = $temporary_directory ? $temporary_directory : file_directory_path() . '/tmp';
-    variable_set('file_directory_temp', $temporary_directory);
-  }
-
-  return $temporary_directory;
-}
-
-/**
- * Determine the default 'files' directory.
+ * This function will return the directory path of a stream wrapper. A stream
+ * is referenced as: "scheme://target".  For example, a scheme of "public"
+ * might return "sites/default/files" or "temporary" might return "/tmp".
  *
+ * @param $scheme
+ *   A string representing the scheme of a stream. The default wrapper is
+ *   is assumed if this is not provided.
  * @return
- *   A string containing the path to Drupal's 'files' directory.
- */
-function file_directory_path() {
-  return variable_get('file_directory_path', conf_path() . '/files');
-}
-
-/**
- * Remove a possible leading file directory path from the given path.
- *
- * @param $path
- *   Path to a file that may be in Drupal's files directory.
- * @return
- *   String with Drupal's files directory removed from it.
+ *   A string containing the directory path of a stream. FALSE is returned if
+ *   the scheme is invalid or a wrapper could not be instantiated.
  */
-function file_directory_strip($path) {
-  // Strip file_directory_path from $path. We only include relative paths in
-  // URLs.
-  if (strpos($path, file_directory_path() . '/') === 0) {
-    $path = trim(substr($path, strlen(file_directory_path())), '\\/');
+function file_directory_path($scheme = NULL) {
+  if (empty($scheme)) {
+    $scheme = variable_get('file_default_scheme', 'public');
+  }
+  if ($wrapper = file_stream_wrapper_get_instance_by_scheme($scheme)) {
+    return $wrapper->getDirectoryPath();
+  }
+  else {
+    return FALSE;
   }
-  return $path;
 }
 
 /**
- * Determine the maximum file upload size by querying the PHP settings.
+ * Determines the maximum file upload size by querying the PHP settings.
  *
  * @return
- *   A file size limit in bytes based on the PHP upload_max_filesize and
- *   post_max_size
+ *   Returns file size limit in bytes based on the PHP upload_max_filesize
+ *   and post_max_size directives.
  */
 function file_upload_max_size() {
   static $max_size = -1;
@@ -1761,56 +1722,38 @@ function file_upload_max_size() {
 }
 
 /**
- * Determine an Internet Media Type, or MIME type from a filename.
+ * Determines an Internet Media Type, or MIME type from a filename.
  *
- * @param $filename
- *   Name of the file, including extension.
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
+ * @param $uri
+ *   A string containing the URI, path, or filename.
  * @param $mapping
  *   An optional map of extensions to their mimetypes, in the form:
  *    - 'mimetypes': a list of mimetypes, keyed by an identifier,
  *    - 'extensions': the mapping itself, an associative array in which
  *      the key is the extension (lowercase) and the value is the mimetype identifier.
- *    If $mapping is omitted, the drupal variable mime_extension_mapping is checked
- *    for a value and if that fails then file_default_mimetype_mapping() is called
- *
+ *   If $mapping is omitted, the drupal variable mime_extension_mapping is checked
+ *   for a value and if that fails then file_default_mimetype_mapping() is called.
  * @return
  *   The internet media type registered for the extension or application/octet-stream for unknown extensions.
  * @see
  *   file_default_mimetype_mapping()
  */
-function file_get_mimetype($filename, $mapping = NULL) {
-  if (!isset($mapping)) {
-    $mapping = variable_get('mime_extension_mapping', NULL);
-    if (!isset($mapping) && drupal_function_exists('file_default_mimetype_mapping')) {
-      // The default file map, defined in file.mimetypes.inc is quite big.
-      // We only load it when necessary.
-      $mapping = file_default_mimetype_mapping();
-    }
+function file_get_mimetype($uri, $mapping = NULL) {
+  if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
+    return $wrapper->getMimeType($uri, $mapping);
   }
-
-  $extension = '';
-  $file_parts = explode('.', $filename);
-
-  // Remove the first part: a full filename should not match an extension.
-  array_shift($file_parts);
-
-  // Iterate over the file parts, trying to find a match.
-  // For my.awesome.image.jpeg, we try:
-  //   - jpeg
-  //   - image.jpeg, and
-  //   - awesome.image.jpeg
-  while ($additional_part = array_pop($file_parts)) {
-    $extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
-    if (isset($mapping['extensions'][$extension])) {
-      return $mapping['mimetypes'][$mapping['extensions'][$extension]];
-    }
+  else {
+    // getMimeType() is not implementation specific, so we can directly
+    // call it without an instance.
+    return DrupalLocalStreamWrapper::getMimeType($uri, $mapping);
   }
-
-  return 'application/octet-stream';
 }
 
 /**
- * Set the permissions on a file or directory.
+ * Sets the permissions on a file or directory.
  *
  * This function will use the 'file_chmod_directory' and 'file_chmod_file'
  * variables for the default modes for directories and uploaded/generated files.
@@ -1819,17 +1762,27 @@ function file_get_mimetype($filename, $mapping = NULL) {
  * 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.
  *
- * @param $path
- *   String containing the path to a file or directory.
+ * 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
+ * bahavior in their implementations as needed.
+ *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
+ * @param $uri
+ *   A string containing the URI to verify. If this value is omitted,
+ *   Drupal's public files directory will be used [public://].
  * @param $mode
  *   Integer value for the permissions. Consult PHP chmod() documentation for
  *   more information.
  * @return
  *   TRUE for success, FALSE in the event of an error.
+ *
+ * @see chmod()
  */
-function drupal_chmod($path, $mode = NULL) {
+function drupal_chmod($uri, $mode = NULL) {
   if (!isset($mode)) {
-    if (is_dir($path)) {
+    if (is_dir($uri)) {
       $mode = variable_get('file_chmod_directory', 0775);
     }
     else {
@@ -1837,14 +1790,169 @@ function drupal_chmod($path, $mode = NULL) {
     }
   }
 
-  if (@chmod($path, $mode)) {
-    return TRUE;
+  // 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 %path.', array('%path' => $path), WATCHDOG_ERROR);
+  watchdog('file', 'The file permissions could not be set on %uri.', array('%uri' => $uri), WATCHDOG_ERROR);
   return FALSE;
 }
 
 /**
+ * Returns the absolute path of a file or directory
+ *
+ * PHP's realpath() does not properly support streams, so this function
+ * fills that gap. If a stream wrapped URI is provided, it will be passed
+ * to the registered wrapper for handling. If the URI does not contain a
+ * scheme or the wrapper implementation does not implement realpath, then
+ * FALSE will be returned.
+ *
+ * @see http://php.net/manual/en/function.realpath.php
+ *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
+ * @param $uri
+ *   A string containing the URI to verify. If this value is omitted,
+ *   Drupal's public files directory will be used [public://].
+ * @return
+ *   Returns the absolute pathname, or FALSE on failure.
+ *
+ * @see 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();
+  }
+  else {
+    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.
+ * @see http://drupal.org/node/515192
+ *
+ * @param $uri
+ *   A URI or path.
+ * @return
+ *   Returns a string containing the directory name.
+ *
+ * @see dirname()
+ */
+function drupal_dirname($uri) {
+	$scheme = file_uri_scheme($uri);
+
+  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
+    $target  = file_uri_target($uri);
+    $dirname = dirname($target);
+
+    if ($dirname == '.') {
+      $dirname = '';
+    }
+
+    return $scheme . '://' . $dirname;
+  }
+  else {
+    return dirname($uri);
+  }
+}
+
+/**
+ * Creates a directory using Drupal's default mode.
+ *
+ * PHP's mkdir() does not respect Drupal's default permissions mode. If a mode
+ * is not provided, this function will make sure that Drupal's is used.
+ *
+ * Compatibility: normal paths and stream wrappers.
+ * @see http://drupal.org/node/515192
+ *
+ * @param $uri
+ *   A URI or pathname.
+ * @param $mode
+ *   By default the Drupal mode is used.
+ * @param $recursive
+ *   Default to FALSE.
+ * @param $context
+ *   Refer to http://php.net/manual/en/ref.stream.php
+ * @return
+ *   Returns TRUE on success or FALSE on failure.
+ *
+ * @see mkdir()
+ */
+function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
+
+  if (is_null($mode)) {
+    $mode = variable_get('file_chmod_directory', 0775);
+  }
+
+  if (is_null($context)) {
+    return mkdir($uri, $mode, $recursive);
+  }
+  else {
+    return mkdir($uri, $mode, $recursive, $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.
+ * @see http://drupal.org/node/515192
+ *
+ * @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
+ *   Returns the new temporary fillename, or FALSE on failure.
+ *
+ * @see tempnam()
+ */
+function drupal_tempnam($directory, $prefix) {
+ 	$scheme = file_uri_scheme($directory);
+
+  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
+    $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
+    
+    if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
+      return $scheme . '://' . basename($filename);
+    }
+    else {
+      return FALSE;
+    }
+  }
+  else {
+    // Handle as a normal tempnam() call.
+    return tempnam($directory, $prefix);
+  }
+}
+
+
+/**
  * @} End of "defgroup file".
  */
diff --git includes/filetransfer/filetransfer.inc includes/filetransfer/filetransfer.inc
index 588526c..e5c9ff8 100644
--- includes/filetransfer/filetransfer.inc
+++ includes/filetransfer/filetransfer.inc
@@ -114,7 +114,7 @@ abstract class FileTransfer {
    *   A path to check against the jail.
    */
   protected final function checkPath($path) {
-    if (realpath(substr($path, 0, strlen($this->jail))) !== $this->jail) {
+    if (drupal_realpath(substr($path, 0, strlen($this->jail))) !== $this->jail) {
       throw new FileTransferException('@directory is outside of the @jail', NULL, array('@directory' => $path, '@jail' => $this->jail));
     }
   }
diff --git includes/filetransfer/ftp.inc includes/filetransfer/ftp.inc
index 51a2e66..45a3a3f 100644
--- includes/filetransfer/ftp.inc
+++ includes/filetransfer/ftp.inc
@@ -19,7 +19,7 @@ class FileTransferFTPWrapper extends FileTransfer {
   }
 
   function createDirectoryJailed($directory) {
-    if (!@mkdir($directory)) {
+    if (!@drupal_mkdir($directory)) {
       $exception = new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
       throw $exception;
     }
diff --git includes/form.inc includes/form.inc
index db9af87..53a9464 100644
--- includes/form.inc
+++ includes/form.inc
@@ -2452,7 +2452,7 @@ function theme_image_button($element) {
     (!empty($element['#value']) ? ('value="' . check_plain($element['#value']) . '" ') : '') .
     'id="' . $element['#id'] . '" ' .
     drupal_attributes($element['#attributes']) .
-    ' src="' . base_path() . $element['#src'] . '" ' .
+    ' src="' . file_external_url($element['#src']) . '" ' .
     (!empty($element['#title']) ? 'alt="' . check_plain($element['#title']) . '" title="' . check_plain($element['#title']) . '" ' : '' ) .
     "/>\n";
 }
diff --git includes/install.inc includes/install.inc
index 43795d5..ebefa32 100644
--- includes/install.inc
+++ includes/install.inc
@@ -216,9 +216,9 @@ function drupal_detect_database_types() {
   // file for the driver explicitly.
   require_once DRUPAL_ROOT . '/includes/database/database.inc';
   foreach (file_scan_directory(DRUPAL_ROOT . '/includes/database', '/^[a-z]*$/i', array('recurse' => FALSE)) as $file) {
-    include_once "{$file->filepath}/install.inc";
-    include_once "{$file->filepath}/database.inc";
-    $drivers[$file->filename] = $file->filepath;
+    include_once "{$file->uri}/install.inc";
+    include_once "{$file->uri}/database.inc";
+    $drivers[$file->filename] = $file->uri;
   }
 
   foreach ($drivers as $driver => $file) {
@@ -810,7 +810,7 @@ function drupal_install_mkdir($file, $mask, $message = TRUE) {
     }
   }
 
-  if (@mkdir($file, intval("0$mod", 8))) {
+  if (@drupal_mkdir($file, intval("0$mod", 8))) {
     return TRUE;
   }
   else {
@@ -934,7 +934,7 @@ function st($string, $args = array()) {
     $filename = 'profiles/' . $install_state['parameters']['profile'] . '/translations/' . $install_state['parameters']['locale'] . '.po';
     if (file_exists(DRUPAL_ROOT . '/' . $filename)) {
       require_once DRUPAL_ROOT . '/includes/locale.inc';
-      $file = (object) array('filepath' => $filename);
+      $file = (object) array('uri' => $filename);
       _locale_import_read_po('mem-store', $file);
       $locale_strings = _locale_import_one_string('mem-report');
     }
@@ -986,7 +986,7 @@ function drupal_check_profile($profile) {
   // Collect requirement testing results
   $requirements = array();
   foreach ($installs as $install) {
-    require_once DRUPAL_ROOT . '/' . $install->filepath;
+    require_once DRUPAL_ROOT . '/' . $install->uri;
     $function = $install->name . '_requirements';
     if (function_exists($function)) {
       $requirements = array_merge($requirements, $function('install'));
@@ -1026,7 +1026,7 @@ function drupal_check_module($module) {
   // Include install file
   $install = drupal_get_install_files(array($module));
   if (isset($install[$module])) {
-    require_once DRUPAL_ROOT . '/' . $install[$module]->filepath;
+    require_once DRUPAL_ROOT . '/' . $install[$module]->uri;
 
     // Check requirements
     $requirements = module_invoke($module, 'requirements', 'install');
diff --git includes/locale.inc includes/locale.inc
index 4e76e96..f540b2f 100644
--- includes/locale.inc
+++ includes/locale.inc
@@ -1223,7 +1223,7 @@ function _locale_import_po($file, $langcode, $mode, $group = NULL) {
  */
 function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = 'default') {
 
-  $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return
+  $fd = fopen($file->uri, "rb"); // File will get closed by PHP on return
   if (!$fd) {
     _locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
     return FALSE;
@@ -1439,7 +1439,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
         $header = _locale_import_parse_header($value['msgstr']);
 
         // Get the plural formula and update in database.
-        if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->filepath)) {
+        if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->uri)) {
           list($nplurals, $plural) = $p;
           db_update('languages')
             ->fields(array(
@@ -2435,11 +2435,11 @@ function _locale_rebuild_js($langcode = NULL) {
 
   // Construct the filepath where JS translation files are stored.
   // There is (on purpose) no front end to edit that variable.
-  $dir = file_create_path(variable_get('locale_js_directory', 'languages'));
+  $dir = 'public://' . variable_get('locale_js_directory', 'languages');
 
   // Delete old file, if we have no translations anymore, or a different file to be saved.
   if (!empty($language->javascript) && (!$data || $language->javascript != $data_hash)) {
-    file_unmanaged_delete(file_create_path($dir . '/' . $language->language . '_' . $language->javascript . '.js'));
+    file_unmanaged_delete($dir . '/' . $language->language . '_' . $language->javascript . '.js');
     $language->javascript = '';
     $status = 'deleted';
   }
@@ -2447,7 +2447,7 @@ function _locale_rebuild_js($langcode = NULL) {
   // Only create a new file if the content has changed.
   if ($data && $language->javascript != $data_hash) {
     // Ensure that the directory exists and is writable, if possible.
-    file_check_directory($dir, TRUE);
+    file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
 
     // Save the file.
     $dest = $dir . '/' . $language->language . '_' . $data_hash . '.js';
@@ -2650,7 +2650,7 @@ function _locale_batch_build($files, $finished = NULL, $components = array()) {
     $operations = array();
     foreach ($files as $file) {
       // We call _locale_batch_import for every batch operation.
-      $operations[] = array('_locale_batch_import', array($file->filepath));
+      $operations[] = array('_locale_batch_import', array($file->uri));
     }
     $batch = array(
       'operations'    => $operations,
@@ -2682,7 +2682,7 @@ function _locale_batch_import($filepath, &$context) {
   // The filename is either {langcode}.po or {prefix}.{langcode}.po, so
   // we can extract the language code to use for the import from the end.
   if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) {
-    $file = (object) array('filename' => basename($filepath), 'filepath' => $filepath);
+    $file = (object) array('filename' => basename($filepath), 'uri' => $filepath);
     _locale_import_read_po('db-store', $file, LOCALE_IMPORT_KEEP, $langcode[2]);
     $context['results'][] = $filepath;
   }
diff --git includes/module.inc includes/module.inc
index a1a3400..b913fae 100644
--- includes/module.inc
+++ includes/module.inc
@@ -540,7 +540,7 @@ function drupal_required_modules() {
   $files = drupal_system_listing('/\.info$/', 'modules', 'name', 0);
   $required = array();
   foreach ($files as $name => $file) {
-    $info = drupal_parse_info_file($file->filepath);
+    $info = drupal_parse_info_file($file->uri);
     if (!empty($info) && !empty($info['required']) && $info['required']) {
       $required[] = $name;
     }
diff --git includes/registry.inc includes/registry.inc
index 7243ff2..6ec637b 100644
--- includes/registry.inc
+++ includes/registry.inc
@@ -42,7 +42,7 @@ function _registry_rebuild() {
   // Get the list of files we are going to parse.
   $files = array();
   foreach ($modules as &$module) {
-    $dir = dirname($module->filepath);
+    $dir = dirname($module->uri);
 
     // Store the module directory for use in hook_registry_files_alter().
     $module->dir = $dir;
diff --git includes/stream_wrappers.inc includes/stream_wrappers.inc
index 75a1f8f..f034769 100644
--- includes/stream_wrappers.inc
+++ includes/stream_wrappers.inc
@@ -226,14 +226,14 @@ abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface
    * Base implementation of chmod().
    */
   function chmod($mode) {
-    return @chmod($this->realpath(), $mode);
+    return @chmod($this->getLocalPath(), $mode);
   }
 
   /**
    * Base implementaiton of realpath().
    */
   function realpath() {
-    return @realpath($this->getDirectoryPath() . '/' . file_uri_target($this->uri));
+    return $this->getLocalPath();
   }
 
   /**
@@ -246,7 +246,17 @@ abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface
     if (!isset($uri)) {
       $uri = $this->uri;
     }
-    return $this->getDirectoryPath() . '/' . file_uri_target($uri);
+    $path = $this->getDirectoryPath() . '/' . file_uri_target($uri);
+    $realpath = realpath($path);
+    if (!$realpath) {
+      // This file does not yet exist.
+      $realpath = realpath(dirname($path)) . '/' . basename($path);
+    }
+    $directory = realpath($this->getDirectoryPath());
+    if (!$directory || strpos($realpath, $directory) !== 0) {
+      return FALSE;
+    }
+    return $realpath;
   }
 
   /**
@@ -440,11 +450,19 @@ abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface
   public function mkdir($uri, $mode, $options) {
     $this->uri = $uri;
     $recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);
+    if ($recursive) {
+      // $this->getLocalPath() fails if $uri has multiple levels of directories
+      // that do not yet exist.
+      $localpath = $this->getDirectoryPath() . '/' . file_uri_target($uri);
+    }
+    else {
+      $localpath = $this->getLocalPath();
+    }
     if ($options & STREAM_REPORT_ERRORS) {
-      return mkdir($this->getLocalPath(), $mode, $recursive);
+      return mkdir($localpath, $mode, $recursive);
     }
     else {
-      return @mkdir($this->getLocalPath(), $mode, $recursive);
+      return @mkdir($localpath, $mode, $recursive);
     }
   }
 
@@ -554,7 +572,7 @@ class DrupalPublicStreamWrapper extends DrupalLocalStreamWrapper {
    * Implements abstract public function getDirectoryPath()
    */
   public function getDirectoryPath() {
-    return variable_get('file_public_path', 'sites/default/files');
+    return variable_get('file_public_path', conf_path() . '/files');
   }
 
   /**
@@ -582,7 +600,7 @@ class DrupalPrivateStreamWrapper extends DrupalLocalStreamWrapper {
    * Implements abstract public function getDirectoryPath()
    */
   public function getDirectoryPath() {
-    return variable_get('file_private_path', 'sites/default/files-private');
+    return variable_get('file_private_path', conf_path() . '/private/files');
   }
 
   /**
@@ -609,13 +627,14 @@ class DrupalTemporaryStreamWrapper extends DrupalLocalStreamWrapper {
    * Implements abstract public function getDirectoryPath()
    */
   public function getDirectoryPath() {
-    return variable_get('file_temporary_path', '/tmp');
+    return variable_get('file_temporary_path', conf_path() . '/private/temp');
   }
 
   /**
    * Overrides getExternalUrl().
    */
   public function getExternalUrl() {
-    return '';
+    $path = str_replace('\\', '/', file_uri_target($this->uri));
+    return url('system/temporary/' . $path, array('absolute' => TRUE));
   }
 }
diff --git includes/theme.inc includes/theme.inc
index 4d561d8..4825f6b 100644
--- includes/theme.inc
+++ includes/theme.inc
@@ -967,7 +967,7 @@ function drupal_find_theme_templates($cache, $extension, $path) {
   $files = drupal_system_listing($regex, $path, 'name', 0);
   foreach ($files as $template => $file) {
     // Ignore sub-theme templates for the current theme.
-    if (strpos($file->filepath, str_replace($subtheme_paths, '', $file->filepath)) !== 0) {
+    if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
       continue;
     }
     // Chop off the remaining extensions if there are any. $template already
@@ -982,7 +982,7 @@ function drupal_find_theme_templates($cache, $extension, $path) {
     if (isset($cache[$hook])) {
       $templates[$hook] = array(
         'template' => $template,
-        'path' => dirname($file->filepath),
+        'path' => dirname($file->uri),
       );
     }
     // Ensure that the pattern is maintained from base themes to its sub-themes.
@@ -1008,7 +1008,7 @@ function drupal_find_theme_templates($cache, $extension, $path) {
           // Put the underscores back in for the hook name and register this pattern.
           $templates[strtr($file, '-', '_')] = array(
             'template' => $file,
-            'path' => dirname($files[$match]->filepath),
+            'path' => dirname($files[$match]->uri),
             'arguments' => $info['arguments'],
           );
         }
@@ -1099,24 +1099,24 @@ function theme_get_setting($setting_name, $refresh = FALSE) {
 
     if ($settings['toggle_logo']) {
       if ($settings['default_logo']) {
-        $settings['logo'] = base_path() . dirname($theme_object->filename) . '/logo.png';
+        $settings['logo'] = file_external_url(dirname($theme_object->filename) . '/logo.png');
       }
       elseif ($settings['logo_path']) {
-        $settings['logo'] = base_path() . $settings['logo_path'];
+        $settings['logo'] = file_external_url($settings['logo_path']);
       }
     }
 
     if ($settings['toggle_favicon']) {
       if ($settings['default_favicon']) {
         if (file_exists($favicon = dirname($theme_object->filename) . '/favicon.ico')) {
-          $settings['favicon'] = base_path() . $favicon;
+          $settings['favicon'] = file_external_url($favicon);
         }
         else {
-          $settings['favicon'] = base_path() . 'misc/favicon.ico';
+          $settings['favicon'] = file_external_url('misc/favicon.ico');
         }
       }
       elseif ($settings['favicon_path']) {
-        $settings['favicon'] = base_path() . $settings['favicon_path'];
+        $settings['favicon'] = file_external_url($settings['favicon_path']);
       }
       else {
         $settings['toggle_favicon'] = FALSE;
@@ -1340,7 +1340,7 @@ function theme_links($links, $attributes = array('class' => 'links')) {
 function theme_image($path, $alt = '', $title = '', $attributes = array(), $getsize = TRUE) {
   if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
     $attributes = drupal_attributes($attributes);
-    $url = (url($path) == $path) ? $path : (base_path() . $path);
+    $url = file_external_url($path);
     return '<img src="' . check_url($url) . '" alt="' . check_plain($alt) . '" title="' . check_plain($title) . '" ' . (isset($image_attributes) ? $image_attributes : '') . $attributes . ' />';
   }
 }
diff --git install.php install.php
index 4d7f98d..26cbac1 100644
--- install.php
+++ install.php
@@ -1039,6 +1039,8 @@ function _install_select_profile($profiles) {
   // Don't need to choose profile if only one available.
   if (sizeof($profiles) == 1) {
     $profile = array_pop($profiles);
+    // TODO: is this right?
+    require_once $profile->uri;
     return $profile->name;
   }
   else {
@@ -1063,7 +1065,8 @@ function install_select_profile_form(&$form_state, $profile_files) {
   $names = array();
 
   foreach ($profile_files as $profile) {
-    include_once DRUPAL_ROOT . '/' . $profile->filepath;
+    // TODO: is this right?
+    include_once DRUPAL_ROOT . '/' . $profile->uri;
     
     $details = install_profile_info($profile->name);
     $profiles[$profile->name] = $details;
diff --git modules/aggregator/aggregator.admin.inc modules/aggregator/aggregator.admin.inc
index c9260a3..da1d0e4 100644
--- modules/aggregator/aggregator.admin.inc
+++ modules/aggregator/aggregator.admin.inc
@@ -297,7 +297,7 @@ function aggregator_form_opml_validate($form, &$form_state) {
 function aggregator_form_opml_submit($form, &$form_state) {
   $data = '';
   if ($file = file_save_upload('upload')) {
-    $data = file_get_contents($file->filepath);
+    $data = file_get_contents($file->uri);
   }
   else {
     $response = drupal_http_request($form_state['values']['remote']);
diff --git modules/aggregator/aggregator.test modules/aggregator/aggregator.test
index 8088eec..0514f47 100644
--- modules/aggregator/aggregator.test
+++ modules/aggregator/aggregator.test
@@ -200,7 +200,7 @@ class AggregatorTestCase extends DrupalWebTestCase {
 </opml>
 EOF;
 
-    $path = file_directory_path() . '/valid-opml.xml';
+    $path = 'public://valid-opml.xml';
     return file_unmanaged_save_data($opml, $path);
   }
 
@@ -217,7 +217,7 @@ EOF;
 </opml>
 EOF;
 
-    $path = file_directory_path() . '/invalid-opml.xml';
+    $path = 'public://invalid-opml.xml';
     return file_unmanaged_save_data($opml, $path);
   }
 
@@ -239,7 +239,7 @@ EOF;
 </opml>
 EOF;
 
-    $path = file_directory_path() . '/empty-opml.xml';
+    $path = 'public://empty-opml.xml';
     return file_unmanaged_save_data($opml, $path);
   }
 
@@ -557,7 +557,7 @@ class ImportOPMLTestCase extends AggregatorTestCase {
     $path = $this->getEmptyOpml();
     $form = array(
       'files[upload]' => $path,
-      'remote' => file_create_url($path),
+      'remote' => file_external_url($path),
     );
     $this->drupalPost('admin/settings/aggregator/add/opml', $form, t('Import'));
     $this->assertRaw(t('You must <em>either</em> upload a file or enter a URL.'), t('Error if both fields are filled.'));
@@ -580,7 +580,7 @@ class ImportOPMLTestCase extends AggregatorTestCase {
     $this->drupalPost('admin/settings/aggregator/add/opml', $form, t('Import'));
     $this->assertText(t('No new feed has been added.'), t('Attempting to upload invalid XML.'));
 
-    $form = array('remote' => file_create_url($this->getEmptyOpml()));
+    $form = array('remote' => file_external_url($this->getEmptyOpml()));
     $this->drupalPost('admin/settings/aggregator/add/opml', $form, t('Import'));
     $this->assertText(t('No new feed has been added.'), t('Attempting to load empty OPML from remote URL.'));
 
diff --git modules/blogapi/blogapi.module modules/blogapi/blogapi.module
index 17d1542..0872adb 100644
--- modules/blogapi/blogapi.module
+++ modules/blogapi/blogapi.module
@@ -459,26 +459,29 @@ function blogapi_metaweblog_new_media_object($blogid, $username, $password, $fil
     $filename = str_replace('.', '_', $filename);
     $filename .= '.' . $final_extension;
   }
-
+  else {
+    $filename = $name;
+  }
+  $uri = file_build_uri($filename);
   $data = $file['bits'];
 
   if (!$data) {
     return blogapi_error(t('No file sent.'));
   }
 
-  if (!$file = file_unmanaged_save_data($data, $filename)) {
+  if (!$file = file_unmanaged_save_data($data, $uri)) {
     return blogapi_error(t('Error storing file.'));
   }
 
   $row = new stdClass();
   $row->uid = $user->uid;
-  $row->filepath = $file;
+  $row->uri = $file;
   $row->filesize = $filesize;
 
   drupal_write_record('blogapi_files', $row);
 
   // Return the successful result.
-  return array('url' => file_create_url($file), 'struct');
+  return array('url' => file_external_url($file), 'struct');
 }
 /**
  * Blogging API callback. Returns a list of the taxonomy terms that can be
diff --git modules/blogapi/blogapi.test modules/blogapi/blogapi.test
index 6a74004..dd3d68c 100644
--- modules/blogapi/blogapi.test
+++ modules/blogapi/blogapi.test
@@ -70,7 +70,7 @@ class BlogAPITestCase extends DrupalWebTestCase {
 
     // Upload file.
     $file = current($this->drupalGetTestFiles('text'));
-    $file_contents = file_get_contents($file->filepath);
+    $file_contents = file_get_contents($file->uri);
     $file = array();
     $file['name'] = $this->randomName() . '.txt';
     $file['type'] = 'text';
diff --git modules/color/color.module modules/color/color.module
index 24078ee..baf3bf6 100644
--- modules/color/color.module
+++ modules/color/color.module
@@ -7,7 +7,7 @@
 function color_help($path, $arg) {
   switch ($path) {
     case 'admin/help#color':
-      $output = '<p>' . t('The color module allows a site administrator to quickly and easily change the color scheme of certain themes. Although not all themes support color module, both Garland (the default theme) and Minnelli were designed to take advantage of its features. By using color module with a compatible theme, you can easily change the color of links, backgrounds, text, and other theme elements. Color module requires that your <a href="@url">file download method</a> be set to public.', array('@url' => url('admin/settings/file-system'))) . '</p>';
+      $output = '<p>' . t('The color module allows a site administrator to quickly and easily change the color scheme of certain themes. Although not all themes support color module, both Garland (the default theme) and Minnelli were designed to take advantage of its features. By using color module with a compatible theme, you can easily change the color of links, backgrounds, text, and other theme elements.', array('@url' => url('admin/settings/file-system'))) . '</p>';
       $output .= '<p>' . t("It is important to remember that color module saves a modified copy of the theme's specified stylesheets in the files directory. This means that if you make any manual changes to your theme's stylesheet, you must save your color settings again, even if they haven't changed. This causes the color module generated version of the stylesheets in the files directory to be recreated using the new version of the original file.") . '</p>';
       $output .= '<p>' . t('To change the color settings for a compatible theme, select the "configure" link for the theme on the <a href="@themes">themes administration page</a>.', array('@themes' => url('admin/appearance'))) . '</p>';
       $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@color">Color module</a>.', array('@color' => 'http://drupal.org/handbook/modules/color/')) . '</p>';
@@ -32,22 +32,15 @@ function color_theme() {
  */
 function color_form_system_theme_settings_alter(&$form, &$form_state) {
   if (color_get_info(arg(4)) && function_exists('gd_info')) {
-    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) != FILE_DOWNLOADS_PUBLIC) {
-      // Disables the color changer when the private download method is used.
-      // TODO: This should be solved in a different way. See issue #181003.
-      drupal_set_message(t('The color picker only works if the <a href="@url">download method</a> is set to public.', array('@url' => url('admin/settings/file-system'))), 'warning');
-    }
-    else {
-      $form['color'] = array(
-        '#type' => 'fieldset',
-        '#title' => t('Color scheme'),
-        '#weight' => -1,
-        '#attributes' => array('id' => 'color_scheme_form'),
-        '#theme' => 'color_scheme_form',
-      );
-      $form['color'] += color_scheme_form($form_state, arg(4));
-      $form['#submit'][] = 'color_scheme_form_submit';
-    }
+    $form['color'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Color scheme'),
+      '#weight' => -1,
+      '#attributes' => array('id' => 'color_scheme_form'),
+      '#theme' => 'color_scheme_form',
+    );
+    $form['color'] += color_scheme_form($form_state, arg(4));
+    $form['#submit'][] = 'color_scheme_form_submit';
   }
 }
 
@@ -300,7 +293,7 @@ function color_scheme_form_submit($form, &$form_state) {
   $paths['color'] = file_directory_path() . '/color';
   $paths['target'] = $paths['color'] . '/' . $id;
   foreach ($paths as $path) {
-    file_check_directory($path, FILE_CREATE_DIRECTORY);
+    file_prepare_directory($path, FILE_CREATE_DIRECTORY);
   }
   $paths['target'] = $paths['target'] . '/';
   $paths['id'] = $id;
diff --git modules/image/image.admin.inc modules/image/image.admin.inc
index 85a7724..c2be0df 100644
--- modules/image/image.admin.inc
+++ modules/image/image.admin.inc
@@ -677,11 +677,10 @@ function theme_image_style_preview($style) {
 
   // Set up preview file information.
   $preview_file = image_style_path($style['name'], $original_path);
-  $preview_path = file_create_path($preview_file);
-  if (!file_exists($preview_path) && image_style_create_derivative($style, $original_path, $preview_file)) {
-    $preview_path = file_create_path($preview_file);
+  if (!file_exists($preview_file)) {
+    image_style_create_derivative($style, $original_path, $preview_file);
   }
-  $preview_image = image_get_info($preview_path);
+  $preview_image = image_get_info($preview_file);
   if ($preview_image['width'] > $preview_image['height']) {
     $preview_width = min($preview_image['width'], $sample_width);
     $preview_height = round($preview_width / $preview_image['width'] * $preview_image['height']);
@@ -709,9 +708,9 @@ function theme_image_style_preview($style) {
 
   // Build the preview of the image style.
   $output .= '<div class="preview-image-wrapper">';
-  $output .= check_plain($style['name']) . ' (' . l(t('view actual size'), file_create_url($preview_file) . '?' . time()) . ')';
+  $output .= check_plain($style['name']) . ' (' . l(t('view actual size'), file_external_url($preview_file) . '?' . time()) . ')';
   $output .= '<div class="preview-image modified-image" style="' . $preview_attributes['style'] . '">';
-  $output .= '<a href="' . file_create_url($preview_file) . '?' . time() . '">' . theme('image', file_create_url($preview_file) . '?' . time(), t('Sample modified image'), '', $preview_attributes, FALSE) . '</a>';
+  $output .= '<a href="' . file_external_url($preview_file) . '?' . time() . '">' . theme('image', file_external_url($preview_file) . '?' . time(), t('Sample modified image'), '', $preview_attributes, FALSE) . '</a>';
   $output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image['height'] . 'px</span></div>';
   $output .= '<div class="width" style="width: ' . $preview_width . 'px"><span>' . $preview_image['width'] . 'px</span></div>';
   $output .= '</div>'; // End preview-image.
diff --git modules/image/image.install modules/image/image.install
index 66f5c31..95a70be 100644
--- modules/image/image.install
+++ modules/image/image.install
@@ -14,7 +14,7 @@ function image_install() {
 
   // Create the styles directory and ensure it's writable.
   $path = file_directory_path() . '/styles';
-  file_check_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
+  file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
 }
 
 /**
diff --git modules/image/image.module modules/image/image.module
index 84f72b7..90e5aa0 100644
--- modules/image/image.module
+++ modules/image/image.module
@@ -200,7 +200,7 @@ function image_file_download($filepath) {
     $original_path = implode('/', $args);
 
     // Check that the file exists and is an image.
-    if ($info = image_get_info(file_create_path($filepath))) {
+    if ($info = image_get_info($filepath)) {
       // Check the permissions of the original to grant access to this image.
       $headers = module_invoke_all('file_download', $original_path);
       if (!in_array(-1, $headers)) {
@@ -219,7 +219,7 @@ function image_file_download($filepath) {
  */
 function image_file_move($file, $source) {
   // Delete any image derivatives at the original image path.
-  image_path_flush($file->filepath);
+  image_path_flush($file->uri);
 }
 
 /**
@@ -227,7 +227,7 @@ function image_file_move($file, $source) {
  */
 function image_file_delete($file) {
   // Delete any image derivatives of this image.
-  image_path_flush($file->filepath);
+  image_path_flush($file->uri);
 }
 
 /**
@@ -237,10 +237,10 @@ function image_file_delete($file) {
  *   The Drupal file path to the original image.
  */
 function image_path_flush($path) {
-  $path = file_directory_strip($path);
   $styles = image_styles();
   foreach ($styles as $style) {
-    if ($path = file_create_path('styles/' . $style['name'] . '/' . $path)) {
+    $path = image_style_path($style['name'], $path);
+    if (file_exists($path)) {
       file_unmanaged_delete($path);
     }
   }
@@ -426,9 +426,10 @@ function image_style_generate() {
   $args = func_get_args();
   $style = array_shift($args);
   $style_name = $style['name'];
+  $scheme = array_shift($args);
   $path = implode('/', $args);
 
-  $source = file_create_path($path);
+  $path = $scheme . '://' . $path;
   $path_md5 = md5($path);
   $destination = image_style_path($style['name'], $path);
 
@@ -459,7 +460,7 @@ function image_style_generate() {
   cache_set($cid, $destination, 'cache_image');
 
   // Try to generate the image.
-  if (image_style_create_derivative($style, $source, $destination)) {
+  if (image_style_create_derivative($style, $path, $destination)) {
     $image = image_load($destination);
     cache_clear_all($cid, 'cache_image');
     file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
@@ -481,17 +482,17 @@ function image_style_generate() {
  * @param $source
  *   Path of the source file.
  * @param $destination
- *   Path of the destination file.
+ *   Path or URI of the destination file.
  * @return
  *   TRUE if an image derivative is generated, FALSE if no image derivative
  *   is generated. NULL if the derivative is being generated.
  */
 function image_style_create_derivative($style, $source, $destination) {
   // Get the folder for the final location of this style.
-  $directory = dirname($destination);
+  $directory = drupal_dirname($destination);
 
   // Build the destination folder tree if it doesn't already exist.
-  if (!file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
+  if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
     watchdog('image', 'Failed to create style directory: %directory', array('%directory' => $directory), WATCHDOG_ERROR);
     return FALSE;
   }
@@ -521,7 +522,7 @@ function image_style_create_derivative($style, $source, $destination) {
  *   An image style array.
  */
 function image_style_flush($style) {
-  $style_directory = realpath(file_directory_path() . '/styles/' . $style['name']);
+  $style_directory = drupal_realpath(variable_get('file_default_scheme', 'public') . '://styles/' . $style['name']);
   if (is_dir($style_directory)) {
     file_unmanaged_delete_recursive($style_directory);
   }
@@ -547,7 +548,7 @@ function image_style_flush($style) {
  *
  * This function is the default image generation method. It returns a URL for
  * an image that can be used in an <img> tag. When the browser requests the
- * image at image/generate/[style_name]/[path] the image is generated if it does
+ * image at image/generate/[style_name]/[scheme]/[path] the image is generated if it does
  * not already exist and then served to the browser. This allows each image to
  * have its own PHP instance (and memory limit) for generation of the new image.
  *
@@ -565,7 +566,7 @@ function image_style_url($style_name, $path) {
 
   // If the image already exists use that rather than regenerating it.
   if (file_exists($destination)) {
-    return file_create_url($destination);
+    return file_external_url($destination);
   }
 
   // Disable page cache for this request. This prevents anonymous users from
@@ -575,9 +576,12 @@ function image_style_url($style_name, $path) {
   // Set a cache entry to grant access to this style/image path. This will be
   // checked by image_style_generate().
   cache_set('access:' . $style_name . ':' . md5($path), 1, 'cache_image', REQUEST_TIME + 600);
+  
+  $scheme = file_uri_scheme($path);
+  $target = file_uri_target($path);
 
   // Generate a callback path for the image.
-  $url = url('image/generate/' . $style_name . '/' . $path, array('absolute' => TRUE));
+  $url = url('image/generate/' . $style_name . '/' . $scheme . '/' . $target, array('absolute' => TRUE));
   return $url;
 }
 
@@ -596,7 +600,10 @@ function image_style_url($style_name, $path) {
  * @see image_style_url()
  */
 function image_style_path($style_name, $path) {
-  return file_directory_path() . '/styles/' . $style_name . '/' . file_directory_strip($path);
+  if ($target = file_uri_target($path)) {
+    $path = $target;
+  }
+  return variable_get('file_default_scheme', 'public') . '://styles/' . $style_name . '/' . $path;
 }
 
 /**
@@ -795,7 +802,7 @@ function theme_image_style($style_name, $path, $alt = '', $title = '', $attribut
   if (!file_exists($style_path)) {
     $style_path = image_style_url($style_name, $path);
   }
-  return theme('image', $style_path, $alt, $title, $attributes, $getsize);
+  return theme('image', file_external_url($style_path), $alt, $title, $attributes, $getsize);
 }
 
 /**
diff --git modules/image/image.test modules/image/image.test
index e1fa6da..11bf861 100644
--- modules/image/image.test
+++ modules/image/image.test
@@ -47,16 +47,17 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
     parent::setUp();
 
     $this->style_name = 'style_foo';
+    $this->scheme     = 'public';
     image_style_save(array('name' => $this->style_name));
 
     // Create the directories for the styles.
-    $status = file_check_directory($d = file_directory_path() . '/styles/' . $this->style_name, FILE_CREATE_DIRECTORY);
+    $status = file_prepare_directory($d = file_directory_path() . '/styles/' . $this->style_name, FILE_CREATE_DIRECTORY);
     $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.' ));
 
     // Create a working copy of the file.
     $file = reset($this->drupalGetTestFiles('image'));
-    $this->image_info = image_get_info($file->filepath);
-    $this->image_filepath = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME);
+    $this->image_info = image_get_info($file->uri);
+    $this->image_filepath = file_unmanaged_copy($file->uri, NULL, FILE_EXISTS_RENAME);
     $this->assertNotIdentical(FALSE, $this->image_filepath, t('Created the without generated image file.'));
   }
 
@@ -65,7 +66,7 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
    */
   function testImageStylePath() {
     $actual = image_style_path($this->style_name, $this->image_filepath);
-    $expected = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_filepath);
+    $expected = $this->scheme . '://styles/' . $this->style_name . '/' . basename($this->image_filepath);
     $this->assertEqual($actual, $expected, t('Got the path for a file.'));
   }
 
@@ -75,9 +76,9 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
   function testImageStyleUrl() {
     // Get the URL of a file that has not been generated yet and try to access
     // it before image_style_url has been called.
-    $generated_path = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_filepath);
+    $generated_path = $this->scheme . '://styles/' . $this->style_name . '/' . basename($this->image_filepath);
     $this->assertFalse(file_exists($generated_path), t('Generated file does not exist.'));
-    $expected_generate_url = url('image/generate/' . $this->style_name . '/' . $this->image_filepath, array('absolute' => TRUE));
+    $expected_generate_url = url('image/generate/' . $this->style_name . '/' . $this->scheme . '/' . basename($this->image_filepath), array('absolute' => TRUE));
     $this->drupalGet($expected_generate_url);
     $this->assertResponse(403, t('Access to generate URL was denied.'));
 
@@ -103,7 +104,7 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
     $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.'));
 
     // Check that the URL points directly to the generated file.
-    $expected_generated_url = file_create_url($generated_path);
+    $expected_generated_url = file_external_url($generated_path);
     $actual_generated_url = image_style_url($this->style_name, $this->image_filepath);
     $this->drupalGet($expected_generated_url);
     $this->assertEqual($actual_generated_url, $expected_generated_url, t('Got the download URL for an existing file.'));
@@ -261,7 +262,7 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase {
     // file directory. Copy over an image on the first run.
     if (!isset($file_path)) {
       $file = reset($this->drupalGetTestFiles('image'));
-      $file_path = file_unmanaged_copy($file->filename);
+      $file_path = file_unmanaged_copy($file->uri);
     }
 
     return image_style_url($style['name'], $file_path) ? $file_path : FALSE;
diff --git modules/locale/locale.install modules/locale/locale.install
index da2711e..b0eccfb 100644
--- modules/locale/locale.install
+++ modules/locale/locale.install
@@ -54,11 +54,11 @@ function locale_update_7000() {
  */
 function locale_uninstall() {
   // Delete all JavaScript translation files.
-  $locale_js_directory = file_create_path(variable_get('locale_js_directory', 'languages'));
+  $locale_js_directory = 'public://' . variable_get('locale_js_directory', 'languages');
   $files = db_query('SELECT language, javascript FROM {languages}');
   foreach ($files as $file) {
     if (!empty($file->javascript)) {
-      file_unmanaged_delete(file_create_path($locale_js_directory . '/' . $file->language . '_' . $file->javascript . '.js'));
+      file_unmanaged_delete($locale_js_directory . '/' . $file->language . '_' . $file->javascript . '.js');
     }
   }
   // Delete the JavaScript translations directory if empty.
diff --git modules/locale/locale.module modules/locale/locale.module
index 11dbc46..1ff5cbe 100644
--- modules/locale/locale.module
+++ modules/locale/locale.module
@@ -532,7 +532,7 @@ function locale_system_update($components) {
 function locale_js_alter(&$javascript) {
   global $language;
 
-  $dir = file_create_path(variable_get('locale_js_directory', 'languages'));
+  $dir = 'public://' . variable_get('locale_js_directory', 'languages');
   $parsed = variable_get('javascript_parsed', array());
   $files = $new_files = FALSE;
 
diff --git modules/locale/locale.test modules/locale/locale.test
index eb3e596..922fc97 100644
--- modules/locale/locale.test
+++ modules/locale/locale.test
@@ -696,7 +696,7 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase {
    *   Additional options to pass to the translation import form.
    */
   function importPoFile($contents, array $options = array()) {
-    $name = tempnam(file_directory_temp(), "po_");
+    $name = tempnam(file_directory_path('temporary'), "po_");
     file_put_contents($name, $contents);
     $options['files[file]'] = $name;
     $this->drupalPost('admin/config/international/translate/import', $options, t('Import'));
@@ -843,7 +843,7 @@ class LocaleExportFunctionalTest extends DrupalWebTestCase {
   function testExportTranslation() {
     // First import some known translations.
     // This will also automatically enable the 'fr' language.
-    $name = tempnam(file_directory_temp(), "po_");
+    $name = tempnam(file_directory_path('temporary'), "po_");
     file_put_contents($name, $this->getPoFile());
     $this->drupalPost('admin/config/international/translate/import', array(
       'langcode' => 'fr',
@@ -953,7 +953,7 @@ class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
     $this->drupalPost('admin/config/international/translate/edit/' . $string->lid, $edit, t('Save translations'));
     _locale_rebuild_js('fr');
     $file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject();
-    $js_file = file_create_path(variable_get('locale_js_directory', 'languages')) . '/fr_' . $file->javascript . '.js';
+    $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $file->javascript . '.js';
     $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
 
     // Disable string caching.
diff --git modules/node/node.api.php modules/node/node.api.php
index 6a12341..351cf09 100644
--- modules/node/node.api.php
+++ modules/node/node.api.php
@@ -746,7 +746,7 @@ function hook_prepare($node) {
   if ($file = file_check_upload($field_name)) {
     $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
     if ($file) {
-      if (!image_get_info($file->filepath)) {
+      if (!image_get_info($file->uri)) {
         form_set_error($field_name, t('Uploaded file is not a valid image'));
         return;
       }
@@ -754,7 +754,7 @@ function hook_prepare($node) {
     else {
       return;
     }
-    $node->images['_original'] = $file->filepath;
+    $node->images['_original'] = $file->uri;
     _image_build_derivatives($node, TRUE);
     $node->new_file = TRUE;
   }
diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php
index 7370c3c..08f5633 100644
--- modules/simpletest/drupal_web_test_case.php
+++ modules/simpletest/drupal_web_test_case.php
@@ -534,7 +534,7 @@ class DrupalUnitTestCase extends DrupalTestCase {
 
     // Generate temporary prefixed database to ensure that tests have a clean starting point.
     $db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
-    $conf['file_directory_path'] = $this->originalFileDirectory . '/' . $db_prefix;
+    $conf['file_public_path'] = $this->originalFileDirectory . '/' . $db_prefix;
 
     // If locale is enabled then t() will try to access the database and
     // subsequently will fail as the database is not accessible.
@@ -549,7 +549,7 @@ class DrupalUnitTestCase extends DrupalTestCase {
   function tearDown() {
     global $db_prefix, $conf;
     if (preg_match('/simpletest\d+/', $db_prefix)) {
-      $conf['file_directory_path'] = $this->originalFileDirectory;
+      $conf['file_public_path'] = $this->originalFileDirectory;
       // Return the database prefix to the original.
       $db_prefix = $this->originalPrefix;
       // Restore modules if necessary.
@@ -807,9 +807,9 @@ class DrupalWebTestCase extends DrupalTestCase {
       // If size is set then remove any files that are not of that size.
       if ($size !== NULL) {
         foreach ($files as $file) {
-          $stats = stat($file->filepath);
+          $stats = stat($file->uri);
           if ($stats['size'] != $size) {
-            unset($files[$file->filepath]);
+            unset($files[$file->uri]);
           }
         }
       }
@@ -822,7 +822,7 @@ class DrupalWebTestCase extends DrupalTestCase {
    * Compare two files based on size and file name.
    */
   protected function drupalCompareFiles($file1, $file2) {
-    $compare_size = filesize($file1->filepath) - filesize($file2->filepath);
+    $compare_size = filesize($file1->uri) - filesize($file2->uri);
     if ($compare_size) {
       // Sort by file size.
       return $compare_size;
@@ -1030,6 +1030,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     $this->originalLanguageDefault = variable_get('language_default');
     $this->originalPrefix = $db_prefix;
     $this->originalFileDirectory = file_directory_path();
+
     $clean_url_original = variable_get('clean_url', 0);
 
     // Generate temporary prefixed database to ensure that tests have a clean starting point.
@@ -1043,7 +1044,7 @@ class DrupalWebTestCase extends DrupalTestCase {
     // Create test directory ahead of installation so fatal errors and debug
     // information can be logged during installation process.
     $directory = $this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10);
-    file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
+    file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
 
     // Log fatal errors.
     ini_set('log_errors', 1);
@@ -1106,9 +1107,18 @@ class DrupalWebTestCase extends DrupalTestCase {
     // default mail handler.
     variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php');
 
-    // Use temporary files directory with the same prefix as database. The
-    // directory will have been created already.
-    variable_set('file_directory_path', $directory);
+    // Use temporary files directory with the same prefix as the database.
+    $public_files_directory  = $this->originalFileDirectory . '/' . $db_prefix;
+    $private_files_directory = $public_files_directory . '/private';
+
+    // Set path variables
+    variable_set('file_public_path', $public_files_directory);
+    variable_set('file_private_path', $private_files_directory);
+
+    // Create the directories
+    $directory = file_directory_path('public://');
+    file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
+    file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY);
 
     drupal_set_time_limit($this->timeLimit);
   }
@@ -1162,9 +1172,9 @@ class DrupalWebTestCase extends DrupalTestCase {
     }
 
     if (preg_match('/simpletest\d+/', $db_prefix)) {
-      // Delete temporary files directory and reset files directory path.
+
+      // Delete temporary files directory.
       file_unmanaged_delete_recursive(file_directory_path());
-      variable_set('file_directory_path', $this->originalFileDirectory);
 
       // Remove all prefixed tables (all the tables in the schema).
       $schema = drupal_get_schema(NULL, TRUE);
@@ -1444,7 +1454,7 @@ class DrupalWebTestCase extends DrupalTestCase {
             // is broken. This is a less than elegant workaround. Alternatives
             // are being explored at #253506.
             foreach ($upload as $key => $file) {
-              $file = realpath($file);
+              $file = drupal_realpath($file);
               if ($file && is_file($file)) {
                 $post[$key] = '@' . $file;
               }
@@ -2542,7 +2552,7 @@ function simpletest_verbose($message, $original_file_directory = NULL) {
     // Clear out the previous log.
     $message = t('Starting verbose log at @time.', array('@time' => format_date(time()))) . "\n";
     $directory = $file_directory . '/simpletest';
-    if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
+    if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
       file_put_contents($directory . '/verbose.html', $message);
     }
   }
diff --git modules/simpletest/simpletest.install modules/simpletest/simpletest.install
index 971e05d..2137922 100644
--- modules/simpletest/simpletest.install
+++ modules/simpletest/simpletest.install
@@ -12,8 +12,8 @@
 function simpletest_install() {
   drupal_install_schema('simpletest');
   // Check for files directory.
-  $path = file_directory_path() . '/simpletest';
-  if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
+  $path = 'public://simpletest';
+  if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
     // Generate binary and text test files.
     $generated = FALSE;
     if (simpletest_get_file_count($path, 'binary') == 0) {
@@ -43,7 +43,7 @@ function simpletest_install() {
     // the test files back easily.
     if (count($files) > count(file_scan_directory($path, '/(html|image|javascript|php|sql)-.*/'))) {
       foreach ($files as $file) {
-        file_unmanaged_copy($file->filepath, $path, FILE_EXISTS_REPLACE);
+        file_unmanaged_copy($file->uri, $path, FILE_EXISTS_REPLACE);
       }
       $generated = TRUE;
     }
@@ -117,7 +117,7 @@ function simpletest_uninstall() {
   $path = file_directory_path() . '/simpletest';
   $files = file_scan_directory($path, '/.*/');
   foreach ($files as $file) {
-    file_unmanaged_delete($file->filepath);
+    file_unmanaged_delete($file->uri);
   }
   rmdir($path);
 }
diff --git modules/simpletest/simpletest.module modules/simpletest/simpletest.module
index 8212423..8af2e86 100644
--- modules/simpletest/simpletest.module
+++ modules/simpletest/simpletest.module
@@ -83,6 +83,19 @@ function simpletest_theme() {
 }
 
 /**
+ * Implementation of hook_stream_wrappers().
+ */
+function simpletest_test_stream_wrappers() {
+  return array(
+    'simpletest' => array(
+      'name' => t('Simpletest files'),
+      'class' => 'DrupalSimpleTestStreamWrapper',
+      'description' => t('Stream Wrapper for Simpletest files.'),
+    ),
+  );
+}
+
+/**
  * Implement hook_js_alter().
  */
 function simpletest_js_alter(&$javascript) {
@@ -252,7 +265,7 @@ function simpletest_last_test_get($test_id) {
  *   Found any entries in log.
  */
 function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALSE) {
-  $log = file_directory_path() . ($during_test ? '' : '/simpletest/' . substr($prefix, 10)) . '/error.log';
+  $log = 'public://' . ($during_test ? '' : '/simpletest/' . substr($prefix, 10)) . '/error.log';
   $found = FALSE;
   if (file_exists($log)) {
     foreach (file($log) as $line) {
@@ -405,12 +418,11 @@ function simpletest_clean_database() {
  * Find all leftover temporary directories and remove them.
  */
 function simpletest_clean_temporary_directories() {
-  $directory = file_directory_path() . '/simpletest';
-  $files = scandir($directory);
+  $files = scandir('public://');
   $count = 0;
   foreach ($files as $file) {
-    $path = "$directory/$file";
-    if (is_dir($path) && is_numeric($file)) {
+    $path = 'public://' . $file;
+    if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) {
       file_unmanaged_delete_recursive($path);
       $count++;
     }
@@ -456,3 +468,4 @@ function simpletest_clean_results_table($test_id = NULL) {
   }
   return FALSE;
 }
+
diff --git modules/simpletest/simpletest.test modules/simpletest/simpletest.test
index e881f4d..7e07734 100644
--- modules/simpletest/simpletest.test
+++ modules/simpletest/simpletest.test
@@ -214,7 +214,7 @@ class SimpleTestFunctionalTest extends DrupalWebTestCase {
           $assertion['file'] = $this->asText($row->td[2]);
           $assertion['line'] = $this->asText($row->td[3]);
           $assertion['function'] = $this->asText($row->td[4]);
-          $ok_url = (url('misc/watchdog-ok.png') == 'misc/watchdog-ok.png') ? 'misc/watchdog-ok.png' : (base_path() . 'misc/watchdog-ok.png');
+          $ok_url = file_external_url('misc/watchdog-ok.png');
           $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
           $results['assertions'][] = $assertion;
         }
diff --git modules/simpletest/tests/common.test modules/simpletest/tests/common.test
index ed8918c..b8e523a 100644
--- modules/simpletest/tests/common.test
+++ modules/simpletest/tests/common.test
@@ -301,7 +301,7 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
     );
 
     $css = drupal_get_css();
-    if (preg_match_all('/href="' . preg_quote(base_path(), '/') . '([^?]+)\?/', $css, $matches)) {
+    if (preg_match_all('/href="' . preg_quote($GLOBALS['base_url'] . '/', '/') . '([^?]+)\?/', $css, $matches)) {
       $result = $matches[1];
     }
     else {
diff --git modules/simpletest/tests/error.test modules/simpletest/tests/error.test
index f9319b9..9e4ed6d 100644
--- modules/simpletest/tests/error.test
+++ modules/simpletest/tests/error.test
@@ -26,21 +26,21 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase {
       '%message' => 'Undefined variable: bananas',
       '%function' => 'error_test_generate_warnings()',
       '%line' => 44,
-      '%file' => realpath('modules/simpletest/tests/error_test.module'),
+      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
     );
     $error_warning = array(
       '%type' => 'Warning',
       '%message' => 'Division by zero',
       '%function' => 'error_test_generate_warnings()',
       '%line' => 46,
-      '%file' => realpath('modules/simpletest/tests/error_test.module'),
+      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
     );
     $error_user_notice = array(
       '%type' => 'User notice',
       '%message' => 'Drupal is awesome',
       '%function' => 'error_test_generate_warnings()',
       '%line' => 48,
-      '%file' => realpath('modules/simpletest/tests/error_test.module'),
+      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
     );
 
     // Set error reporting to collect notices.
@@ -77,14 +77,14 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase {
       '%message' => 'Drupal is awesome',
       '%function' => 'error_test_trigger_exception()',
       '%line' => 57,
-      '%file' => realpath('modules/simpletest/tests/error_test.module'),
+      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
     );
     $error_pdo_exception = array(
       '%type' => 'PDOException',
       '%message' => 'SELECT * FROM bananas_are_awesome',
       '%function' => 'error_test_trigger_pdo_exception()',
       '%line' => 65,
-      '%file' => realpath('modules/simpletest/tests/error_test.module'),
+      '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'),
     );
 
     $this->drupalGet('error-test/trigger-exception');
diff --git modules/simpletest/tests/file.test modules/simpletest/tests/file.test
index ad6e22a..c6af1fe 100644
--- modules/simpletest/tests/file.test
+++ modules/simpletest/tests/file.test
@@ -61,7 +61,7 @@ class FileTestCase extends DrupalWebTestCase {
     $this->assertEqual($before->fid, $after->fid, t('File id is the same: %file1 == %file2.', array('%file1' => $before->fid, '%file2' => $after->fid)), 'File unchanged');
     $this->assertEqual($before->uid, $after->uid, t('File owner is the same: %file1 == %file2.', array('%file1' => $before->uid, '%file2' => $after->uid)), 'File unchanged');
     $this->assertEqual($before->filename, $after->filename, t('File name is the same: %file1 == %file2.', array('%file1' => $before->filename, '%file2' => $after->filename)), 'File unchanged');
-    $this->assertEqual($before->filepath, $after->filepath, t('File path is the same: %file1 == %file2.', array('%file1' => $before->filepath, '%file2' => $after->filepath)), 'File unchanged');
+    $this->assertEqual($before->uri, $after->uri, t('File path is the same: %file1 == %file2.', array('%file1' => $before->uri, '%file2' => $after->uri)), 'File unchanged');
     $this->assertEqual($before->filemime, $after->filemime, t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->filemime, '%file2' => $after->filemime)), 'File unchanged');
     $this->assertEqual($before->filesize, $after->filesize, t('File size is the same: %file1 == %file2.', array('%file1' => $before->filesize, '%file2' => $after->filesize)), 'File unchanged');
     $this->assertEqual($before->status, $after->status, t('File status is the same: %file1 == %file2.', array('%file1' => $before->status, '%file2' => $after->status)), 'File unchanged');
@@ -77,7 +77,7 @@ class FileTestCase extends DrupalWebTestCase {
    */
   function assertDifferentFile($file1, $file2) {
     $this->assertNotEqual($file1->fid, $file2->fid, t('Files have different ids: %file1 != %file2.', array('%file1' => $file1->fid, '%file2' => $file2->fid)), 'Different file');
-    $this->assertNotEqual($file1->filepath, $file2->filepath, t('Files have different paths: %file1 != %file2.', array('%file1' => $file1->filepath, '%file2' => $file2->filepath)), 'Different file');
+    $this->assertNotEqual($file1->uri, $file2->uri, t('Files have different paths: %file1 != %file2.', array('%file1' => $file1->uri, '%file2' => $file2->uri)), 'Different file');
   }
 
   /**
@@ -90,7 +90,7 @@ class FileTestCase extends DrupalWebTestCase {
    */
   function assertSameFile($file1, $file2) {
     $this->assertEqual($file1->fid, $file2->fid, t('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->fid, '%file2-fid' => $file2->fid)), 'Same file');
-    $this->assertEqual($file1->filepath, $file2->filepath, t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->filepath, '%file2' => $file2->filepath)), 'Same file');
+    $this->assertEqual($file1->uri, $file2->uri, t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->uri, '%file2' => $file2->uri)), 'Same file');
   }
 
   /**
@@ -151,7 +151,7 @@ class FileTestCase extends DrupalWebTestCase {
     if (is_null($path)) {
       $path = file_directory_path() . '/' . $this->randomName();
     }
-    $this->assertTrue(mkdir($path) && is_dir($path), t('Directory was created successfully.'));
+    $this->assertTrue(drupal_mkdir($path) && is_dir($path), t('Directory was created successfully.'));
     return $path;
   }
 
@@ -159,38 +159,42 @@ class FileTestCase extends DrupalWebTestCase {
    * Create a file and save it to the files table and assert that it occurs
    * correctly.
    *
-   * @param $filepath
+   * @param $path
    *   Optional string specifying the file path. If none is provided then a
-   *   randomly named file will be created in the site's files directory.
+   *   randomly named file will be created.
    * @param $contents
    *   Optional contents to save into the file. If a NULL value is provided an
    *   arbitrary string will be used.
+   * @param $scheme
+   *   Optionaly string indicating the stream scheme to use. Drupal core includes
+   *   public://, private://, and temporary://. The public:// wrapper is the default.
    * @return
    *   File object.
    */
-  function createFile($filepath = NULL, $contents = NULL) {
-    if (is_null($filepath)) {
-      $filepath = file_directory_path() . '/' . $this->randomName();
+  function createFile($path = NULL, $contents = NULL, $scheme = 'public://') {
+    if (is_null($path)) {
+      $path = $this->randomName();
     }
+    $path = $scheme . $path;
 
     if (is_null($contents)) {
       $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
     }
 
-    file_put_contents($filepath, $contents);
-    $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
+    file_put_contents($path, $contents);
+    $this->assertTrue(is_file($path), t('The test file exists on the disk.'), 'Create test file');
 
     $file = new stdClass();
-    $file->filepath = $filepath;
-    $file->filename = basename($file->filepath);
-    $file->filemime = 'text/plain';
-    $file->uid = 1;
+    $file->uri       = $path;
+    $file->filename  = basename($file->uri);
+    $file->filemime  = 'text/plain';
+    $file->uid       = 1;
     $file->timestamp = REQUEST_TIME;
-    $file->filesize = filesize($file->filepath);
-    $file->status = 0;
+    $file->filesize  = filesize($file->uri);
+    $file->status    = 0;
     // Write the record directly rather than calling file_save() so we don't
     // invoke the hooks.
-    $this->assertNotIdentical(drupal_write_record('files', $file), FALSE, t('The file was added to the database.'), 'Create test file');
+    $this->assertNotIdentical(drupal_write_record('file', $file), FALSE, t('The file was added to the database.'), 'Create test file');
 
     return $file;
   }
@@ -284,16 +288,16 @@ class FileSpaceUsedTest extends FileTestCase {
     parent::setUp();
 
     // Create records for a couple of users with different sizes.
-    drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT));
-    drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT));
-    drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT));
-    drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT));
+    drupal_write_record('file', $file = array('uid' => 2, 'uri' => 'public://example1.txt', 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT));
+    drupal_write_record('file', $file = array('uid' => 2, 'uri' => 'public://example2.txt', 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT));
+    drupal_write_record('file', $file = array('uid' => 3, 'uri' => 'public://example3.txt', 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT));
+    drupal_write_record('file', $file = array('uid' => 3, 'uri' => 'public://example4.txt', 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT));
 
     // Now create some with other statuses. These values were chosen arbitrarily
     // for the sole purpose of testing that bitwise operators were used
     // correctly on the field.
-    drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 1, 'status' => 2 | 8));
-    drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 3, 'status' => 2 | 4));
+    drupal_write_record('file', $file = array('uid' => 2, 'uri' => 'public://example5.txt', 'filesize' => 1, 'status' => 2 | 8));
+    drupal_write_record('file', $file = array('uid' => 3, 'uri' => 'public://example6.txt', 'filesize' => 3, 'status' => 2 | 4));
   }
 
   /**
@@ -344,12 +348,12 @@ class FileValidatorTest extends DrupalWebTestCase {
     parent::setUp();
 
     $this->image = new stdClass();
-    $this->image->filepath = 'misc/druplicon.png';
-    $this->image->filename = basename($this->image->filepath);
+    $this->image->uri = 'misc/druplicon.png';
+    $this->image->filename = basename($this->image->uri);
 
     $this->non_image = new stdClass();
-    $this->non_image->filepath = 'misc/jquery.js';
-    $this->non_image->filename = basename($this->non_image->filepath);
+    $this->non_image->uri = 'misc/jquery.js';
+    $this->non_image->filename = basename($this->non_image->uri);
   }
 
   /**
@@ -370,11 +374,11 @@ class FileValidatorTest extends DrupalWebTestCase {
    *  This ensures a specific file is actually an image.
    */
   function testFileValidateIsImage() {
-    $this->assertTrue(file_exists($this->image->filepath), t('The image being tested exists.'), 'File');
+    $this->assertTrue(file_exists($this->image->uri), t('The image being tested exists.'), 'File');
     $errors = file_validate_is_image($this->image);
     $this->assertEqual(count($errors), 0, t('No error reported for our image file.'), 'File');
 
-    $this->assertTrue(file_exists($this->non_image->filepath), t('The non-image being tested exists.'), 'File');
+    $this->assertTrue(file_exists($this->non_image->uri), t('The non-image being tested exists.'), 'File');
     $errors = file_validate_is_image($this->non_image);
     $this->assertEqual(count($errors), 1, t('An error reported for our non-image file.'), 'File');
   }
@@ -403,18 +407,18 @@ class FileValidatorTest extends DrupalWebTestCase {
     // Maximum size.
     if (image_get_toolkit()) {
       // Copy the image so that the original doesn't get resized.
-      $temp_dir = file_directory_temp();
-      copy(realpath('misc/druplicon.png'), realpath($temp_dir) . '/druplicon.png');
-      $this->image->filepath = $temp_dir . '/druplicon.png';
+      $temp_dir = file_directory_path('temporary');
+      copy(drupal_realpath('misc/druplicon.png'), drupal_realpath($temp_dir) . '/druplicon.png');
+      $this->image->uri = $temp_dir . '/druplicon.png';
 
       $errors = file_validate_image_resolution($this->image, '10x5');
       $this->assertEqual(count($errors), 0, t('No errors should be reported when an oversized image can be scaled down.'), 'File');
 
-      $info = image_get_info($this->image->filepath);
+      $info = image_get_info($this->image->uri);
       $this->assertTrue($info['width'] <= 10, t('Image scaled to correct width.'), 'File');
       $this->assertTrue($info['height'] <= 5, t('Image scaled to correct height.'), 'File');
 
-      unlink(realpath($temp_dir . '/druplicon.png'));
+      unlink(drupal_realpath($temp_dir . '/druplicon.png'));
     }
     else {
       // TODO: should check that the error is returned if no toolkit is available.
@@ -431,15 +435,15 @@ class FileValidatorTest extends DrupalWebTestCase {
     $file = new stdClass();
 
     // Add a filename with an allowed length and test it.
-    $file->filename = str_repeat('x', 255);
-    $this->assertEqual(strlen($file->filename), 255);
+    $file->filename = str_repeat('x', 240);
+    $this->assertEqual(strlen($file->filename), 240);
     $errors = file_validate_name_length($file);
-    $this->assertEqual(count($errors), 0, t('No errors reported for 255 length filename.'), 'File');
+    $this->assertEqual(count($errors), 0, t('No errors reported for 240 length filename.'), 'File');
 
     // Add a filename with a length too long and test it.
-    $file->filename = str_repeat('x', 256);
+    $file->filename = str_repeat('x', 241);
     $errors = file_validate_name_length($file);
-    $this->assertEqual(count($errors), 1, t('An error reported for 256 length filename.'), 'File');
+    $this->assertEqual(count($errors), 1, t('An error reported for 241 length filename.'), 'File');
 
     // Add a filename with an empty string and test it.
     $file->filename = '';
@@ -507,15 +511,15 @@ class FileUnmanagedSaveDataTest extends FileTestCase {
     // No filename.
     $filepath = file_unmanaged_save_data($contents);
     $this->assertTrue($filepath, t('Unnamed file saved correctly.'));
-    $this->assertEqual(file_directory_path(), dirname($filepath), t("File was placed in Drupal's files directory."));
-    $this->assertEqual($contents, file_get_contents(realpath($filepath)), t('Contents of the file are correct.'));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(file_stream_wrapper_valid_scheme($filepath)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));
 
     // Provide a filename.
-    $filepath = file_unmanaged_save_data($contents, 'asdf.txt', FILE_EXISTS_REPLACE);
+    $filepath = file_unmanaged_save_data($contents, 'public://asdf.txt', FILE_EXISTS_REPLACE);
     $this->assertTrue($filepath, t('Unnamed file saved correctly.'));
-    $this->assertEqual(file_directory_path(), dirname($filepath), t("File was placed in Drupal's files directory."));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(drupal_dirname($filepath)), t("File was placed in Drupal's files directory."));
     $this->assertEqual('asdf.txt', basename($filepath), t('File was named correctly.'));
-    $this->assertEqual($contents, file_get_contents(realpath($filepath)), t('Contents of the file are correct.'));
+    $this->assertEqual($contents, file_get_contents(drupal_realpath($filepath)), t('Contents of the file are correct.'));
     $this->assertFilePermissions($filepath, variable_get('file_chmod_file', 0664));
   }
 }
@@ -548,14 +552,14 @@ class FileSaveUploadTest extends FileHookTestCase {
     $this->drupalLogin($account);
 
     $this->image = current($this->drupalGetTestFiles('image'));
-    $this->assertTrue(is_file($this->image->filepath), t("The file we're going to upload exists."));
+    $this->assertTrue(is_file($this->image->uri), t("The file we're going to upload exists."));
 
-    $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
+    $this->maxFidBefore = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
 
     // Upload with replace to gurantee there's something there.
     $edit = array(
       'file_test_replace' => FILE_EXISTS_REPLACE,
-      'files[file_test_upload]' => realpath($this->image->filepath)
+      'files[file_test_upload]' => drupal_realpath($this->image->uri)
     );
     $this->drupalPost('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, t('Received a 200 response for posted test file.'));
@@ -571,7 +575,7 @@ class FileSaveUploadTest extends FileHookTestCase {
    * Test the file_save_upload() function.
    */
   function testNormal() {
-    $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
+    $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
     $this->assertTrue($max_fid_after > $this->maxFidBefore, t('A new file was created.'));
     $file1 = file_load($max_fid_after);
     $this->assertTrue($file1, t('Loaded the file.'));
@@ -580,13 +584,13 @@ class FileSaveUploadTest extends FileHookTestCase {
     file_test_reset();
 
     // Upload a second file.
-    $max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
+    $max_fid_before = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
     $image2 = current($this->drupalGetTestFiles('image'));
-    $edit = array('files[file_test_upload]' => realpath($image2->filepath));
+    $edit = array('files[file_test_upload]' => drupal_realpath($image2->uri));
     $this->drupalPost('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, t('Received a 200 response for posted test file.'));
     $this->assertRaw(t('You WIN!'));
-    $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {files}')->fetchField();
+    $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file}')->fetchField();
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('validate', 'insert'));
@@ -607,7 +611,7 @@ class FileSaveUploadTest extends FileHookTestCase {
   function testExistingRename() {
     $edit = array(
       'file_test_replace' => FILE_EXISTS_RENAME,
-      'files[file_test_upload]' => realpath($this->image->filepath)
+      'files[file_test_upload]' => drupal_realpath($this->image->uri)
     );
     $this->drupalPost('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, t('Received a 200 response for posted test file.'));
@@ -623,7 +627,7 @@ class FileSaveUploadTest extends FileHookTestCase {
   function testExistingReplace() {
     $edit = array(
       'file_test_replace' => FILE_EXISTS_REPLACE,
-      'files[file_test_upload]' => realpath($this->image->filepath)
+      'files[file_test_upload]' => drupal_realpath($this->image->uri)
     );
     $this->drupalPost('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, t('Received a 200 response for posted test file.'));
@@ -639,7 +643,7 @@ class FileSaveUploadTest extends FileHookTestCase {
   function testExistingError() {
     $edit = array(
       'file_test_replace' => FILE_EXISTS_ERROR,
-      'files[file_test_upload]' => realpath($this->image->filepath)
+      'files[file_test_upload]' => drupal_realpath($this->image->uri)
     );
     $this->drupalPost('file-test/upload', $edit, t('Submit'));
     $this->assertResponse(200, t('Received a 200 response for posted test file.'));
@@ -679,95 +683,55 @@ class FileDirectoryTest extends FileTestCase {
     $this->assertFalse(is_dir($directory), t('Directory does not exist prior to testing.'));
 
     // Non-existent directory.
-    $form_element = $this->randomName();
-    $this->assertFalse(file_check_directory($directory, 0, $form_element), t('Error reported for non-existing directory.'), 'File');
-
-    // Check that an error was set for the form element above.
-    $errors = form_get_errors();
-    $this->assertEqual($errors[$form_element], t('The directory %directory does not exist.', array('%directory' => $directory)), t('Properly generated an error for the passed form element.'), 'File');
+    $this->assertFalse(file_prepare_directory($directory, 0), t('Error reported for non-existing directory.'), 'File');
 
     // Make a directory.
-    $this->assertTrue(file_check_directory($directory, FILE_CREATE_DIRECTORY), t('No error reported when creating a new directory.'), 'File');
+    $this->assertTrue(file_prepare_directory($directory, FILE_CREATE_DIRECTORY), t('No error reported when creating a new directory.'), 'File');
 
     // Make sure directory actually exists.
     $this->assertTrue(is_dir($directory), t('Directory actually exists.'), 'File');
 
     // Make directory read only.
     @chmod($directory, 0444);
-    $form_element = $this->randomName();
-    $this->assertFalse(file_check_directory($directory, 0, $form_element), t('Error reported for a non-writeable directory.'), 'File');
-
-    // Check if form error was set.
-    $errors = form_get_errors();
-    $this->assertEqual($errors[$form_element], t('The directory %directory is not writable', array('%directory' => $directory)), t('Properly generated an error for the passed form element.'), 'File');
+    $this->assertFalse(file_prepare_directory($directory, 0), t('Error reported for a non-writeable directory.'), 'File');
 
     // Test directory permission modification.
-    $this->assertTrue(file_check_directory($directory, FILE_MODIFY_PERMISSIONS), t('No error reported when making directory writeable.'), 'File');
+    $this->assertTrue(file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS), t('No error reported when making directory writeable.'), 'File');
 
     // Test directory permission modification actually set correct permissions.
     $this->assertDirectoryPermissions($directory, variable_get('file_chmod_directory', 0775));
 
     // Remove .htaccess file to then test that it gets re-created.
-    @unlink(file_directory_path() . '/.htaccess');
     $directory = file_directory_path();
-    file_check_directory($directory);
-    $this->assertTrue(is_file(file_directory_path() . '/.htaccess'), t('Successfully created the .htaccess file in the files directory.'), 'File');
-
+    @unlink($directory . '/.htaccess');
+    $this->assertFalse(is_file($directory . '/.htaccess'), t('Successfully removed the .htaccess file in the files directory.'), 'File');
+    file_insure_htaccess();
+    $this->assertTrue(is_file($directory . '/.htaccess'), t('Successfully re-created the .htaccess file in the files directory.'), 'File');
     // Verify contents of .htaccess file.
     $file = file_get_contents(file_directory_path() . '/.htaccess');
     $this->assertEqual($file, "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks", t('The .htaccess file contains the proper content.'), 'File');
   }
 
   /**
-   * Check file_directory_path() and file_directory_temp().
+   * Check file_directory_path('public').
    */
   function testFileDirectoryPath() {
     // Directory path.
-    $path = variable_get('file_directory_path', conf_path() . '/files');
-    $this->assertEqual($path, file_directory_path(), t('Properly returns the stored file directory path.'), 'File');
+    $path = variable_get('file_public_path', '');
+    $this->assertEqual($path, file_directory_path('public'), t('Properly returns the stored file directory path.'), 'File');
   }
 
   /**
-   * Check file_directory_path() and file_directory_temp().
+   * Check file_directory_path() and file_directory_path('temporary').
    */
   function testFileDirectoryTemp() {
     // Temporary directory handling.
     variable_set('file_directory_temp', NULL);
-    $temp = file_directory_temp();
+    $temp = file_directory_path('temporary');
     $this->assertTrue(!is_null($temp), t('Properly set and retrieved temp directory %directory.', array('%directory' => $temp)), 'File');
   }
 
   /**
-   * This tests that a file is actually in the specified directory, to prevent
-   * exploits.
-   */
-  function testFileCheckLocation() {
-    $source = 'misc/xyz.txt';
-    $directory = 'misc';
-    $result = file_check_location($source, $directory);
-    $this->assertTrue($result, t('Non-existent file validates when checked for location in existing directory.'), 'File');
-
-    $source = 'fake/xyz.txt';
-    $directory = 'fake';
-    $result = file_check_location($source, $directory);
-    $this->assertTrue($result, t('Non-existent file validates when checked for location in non-existing directory.'), 'File');
-
-    $source = 'misc/../install.php';
-    $directory = 'misc';
-    $result = file_check_location($source, $directory);
-    $this->assertFalse($result, t('Existing file fails validation when it exists outside the directory path, using a /../ exploit.'), 'File');
-
-    $source = 'misc/druplicon.png';
-    $directory = 'misc';
-    $result = file_check_location($source, $directory);
-    $this->assertTrue($result, t('Existing file passes validation when checked for location in directory path, and filepath contains a subfolder of the checked path.'), 'File');
-
-    $result = file_check_location($source, $directory);
-    $this->assertTrue($result, t('Existing file passes validation, returning the source when checked for location in directory.'), 'File');
-  }
-
-
-  /**
    * This will take a directory and path, and find a valid filepath that is not
    * taken by another file.
    */
@@ -852,15 +816,15 @@ class FileScanDirectoryTest extends FileTestCase {
 
     // Check the first file.
     $file = reset($all_files);
-    $this->assertEqual(key($all_files), $file->filepath, t('Correct array key was used for the first returned file.'));
-    $this->assertEqual($file->filepath, $this->path . '/javascript-1.txt', t('First file name was set correctly.'));
+    $this->assertEqual(key($all_files), $file->uri, t('Correct array key was used for the first returned file.'));
+    $this->assertEqual($file->uri, $this->path . '/javascript-1.txt', t('First file name was set correctly.'));
     $this->assertEqual($file->filename, 'javascript-1.txt', t('First basename was set correctly'));
     $this->assertEqual($file->name, 'javascript-1', t('First name was set correctly.'));
 
     // Check the second file.
     $file = next($all_files);
-    $this->assertEqual(key($all_files), $file->filepath, t('Correct array key was used for the second returned file.'));
-    $this->assertEqual($file->filepath, $this->path . '/javascript-2.script', t('Second file name was set correctly.'));
+    $this->assertEqual(key($all_files), $file->uri, t('Correct array key was used for the second returned file.'));
+    $this->assertEqual($file->uri, $this->path . '/javascript-2.script', t('Second file name was set correctly.'));
     $this->assertEqual($file->filename, 'javascript-2.script', t('Second basename was set correctly'));
     $this->assertEqual($file->name, 'javascript-2', t('Second name was set correctly.'));
   }
@@ -973,8 +937,8 @@ class FileUnmanagedDeleteTest extends FileTestCase {
     $file = $this->createFile();
 
     // Delete a regular file
-    $this->assertTrue(file_unmanaged_delete($file->filepath), t('Deleted worked.'));
-    $this->assertFalse(file_exists($file->filepath), t('Test file has actually been deleted.'));
+    $this->assertTrue(file_unmanaged_delete($file->uri), t('Deleted worked.'));
+    $this->assertFalse(file_exists($file->uri), t('Test file has actually been deleted.'));
   }
 
   /**
@@ -1096,16 +1060,16 @@ class FileUnmanagedMoveTest extends FileTestCase {
     $file = $this->createFile();
 
     // Moving to a new name.
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
-    $new_filepath = file_unmanaged_move($file->filepath, $desired_filepath, FILE_EXISTS_ERROR);
+    $desired_filepath = 'public://' . $this->randomName();
+    $new_filepath = file_unmanaged_move($file->uri, $desired_filepath, FILE_EXISTS_ERROR);
     $this->assertTrue($new_filepath, t('Move was successful.'));
     $this->assertEqual($new_filepath, $desired_filepath, t('Returned expected filepath.'));
     $this->assertTrue(file_exists($new_filepath), t('File exists at the new location.'));
-    $this->assertFalse(file_exists($file->filepath), t('No file remains at the old location.'));
+    $this->assertFalse(file_exists($file->uri), t('No file remains at the old location.'));
     $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
 
     // Moving with rename.
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
+    $desired_filepath = 'public://' . $this->randomName();
     $this->assertTrue(file_exists($new_filepath), t('File exists before moving.'));
     $this->assertTrue(file_put_contents($desired_filepath, ' '), t('Created a file so a rename will have to happen.'));
     $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME);
@@ -1116,6 +1080,7 @@ class FileUnmanagedMoveTest extends FileTestCase {
     $this->assertFilePermissions($newer_filepath, variable_get('file_chmod_file', 0664));
 
     // TODO: test moving to a directory (rather than full directory/file path)
+    // TODO: test creating and moving normal files (rather than streams)
   }
 
   /**
@@ -1135,14 +1100,14 @@ class FileUnmanagedMoveTest extends FileTestCase {
     $file = $this->createFile();
 
     // Move the file onto itself without renaming shouldn't make changes.
-    $new_filepath = file_unmanaged_move($file->filepath, $file->filepath, FILE_EXISTS_REPLACE);
+    $new_filepath = file_unmanaged_move($file->uri, $file->uri, FILE_EXISTS_REPLACE);
     $this->assertFalse($new_filepath, t('Moving onto itself without renaming fails.'));
-    $this->assertTrue(file_exists($file->filepath), t('File exists after moving onto itself.'));
+    $this->assertTrue(file_exists($file->uri), t('File exists after moving onto itself.'));
 
     // Move the file onto itself with renaming will result in a new filename.
-    $new_filepath = file_unmanaged_move($file->filepath, $file->filepath, FILE_EXISTS_RENAME);
+    $new_filepath = file_unmanaged_move($file->uri, $file->uri, FILE_EXISTS_RENAME);
     $this->assertTrue($new_filepath, t('Moving onto itself with renaming works.'));
-    $this->assertFalse(file_exists($file->filepath), t('Original file has been removed.'));
+    $this->assertFalse(file_exists($file->uri), t('Original file has been removed.'));
     $this->assertTrue(file_exists($new_filepath), t('File exists after moving onto itself.'));
   }
 }
@@ -1168,25 +1133,26 @@ class FileUnmanagedCopyTest extends FileTestCase {
     $file = $this->createFile();
 
     // Copying to a new name.
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
-    $new_filepath = file_unmanaged_copy($file->filepath, $desired_filepath, FILE_EXISTS_ERROR);
+    $desired_filepath = 'public://' . $this->randomName();
+    $new_filepath = file_unmanaged_copy($file->uri, $desired_filepath, FILE_EXISTS_ERROR);
     $this->assertTrue($new_filepath, t('Copy was successful.'));
     $this->assertEqual($new_filepath, $desired_filepath, t('Returned expected filepath.'));
-    $this->assertTrue(file_exists($file->filepath), t('Original file remains.'));
+    $this->assertTrue(file_exists($file->uri), t('Original file remains.'));
     $this->assertTrue(file_exists($new_filepath), t('New file exists.'));
     $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
 
     // Copying with rename.
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
+    $desired_filepath = 'public://' . $this->randomName();
     $this->assertTrue(file_put_contents($desired_filepath, ' '), t('Created a file so a rename will have to happen.'));
-    $newer_filepath = file_unmanaged_copy($file->filepath, $desired_filepath, FILE_EXISTS_RENAME);
+    $newer_filepath = file_unmanaged_copy($file->uri, $desired_filepath, FILE_EXISTS_RENAME);
     $this->assertTrue($newer_filepath, t('Copy was successful.'));
     $this->assertNotEqual($newer_filepath, $desired_filepath, t('Returned expected filepath.'));
-    $this->assertTrue(file_exists($file->filepath), t('Original file remains.'));
+    $this->assertTrue(file_exists($file->uri), t('Original file remains.'));
     $this->assertTrue(file_exists($newer_filepath), t('New file exists.'));
     $this->assertFilePermissions($newer_filepath, variable_get('file_chmod_file', 0664));
 
     // TODO: test copying to a directory (rather than full directory/file path)
+    // TODO: test copying normal files using normal paths (rather than only streams)
   }
 
   /**
@@ -1208,28 +1174,28 @@ class FileUnmanagedCopyTest extends FileTestCase {
     $file = $this->createFile();
 
     // Copy the file onto itself with renaming works.
-    $new_filepath = file_unmanaged_copy($file->filepath, $file->filepath, FILE_EXISTS_RENAME);
+    $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_RENAME);
     $this->assertTrue($new_filepath, t('Copying onto itself with renaming works.'));
-    $this->assertNotEqual($new_filepath, $file->filepath, t('Copied file has a new name.'));
-    $this->assertTrue(file_exists($file->filepath), t('Original file exists after copying onto itself.'));
+    $this->assertNotEqual($new_filepath, $file->uri, t('Copied file has a new name.'));
+    $this->assertTrue(file_exists($file->uri), t('Original file exists after copying onto itself.'));
     $this->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.'));
     $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
 
     // Copy the file onto itself without renaming fails.
-    $new_filepath = file_unmanaged_copy($file->filepath, $file->filepath, FILE_EXISTS_ERROR);
+    $new_filepath = file_unmanaged_copy($file->uri, $file->uri, FILE_EXISTS_ERROR);
     $this->assertFalse($new_filepath, t('Copying onto itself without renaming fails.'));
-    $this->assertTrue(file_exists($file->filepath), t('File exists after copying onto itself.'));
+    $this->assertTrue(file_exists($file->uri), t('File exists after copying onto itself.'));
 
     // Copy the file into same directory without renaming fails.
-    $new_filepath = file_unmanaged_copy($file->filepath, dirname($file->filepath), FILE_EXISTS_ERROR);
+    $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_ERROR);
     $this->assertFalse($new_filepath, t('Copying onto itself fails.'));
-    $this->assertTrue(file_exists($file->filepath), t('File exists after copying onto itself.'));
+    $this->assertTrue(file_exists($file->uri), t('File exists after copying onto itself.'));
 
     // Copy the file into same directory with renaming works.
-    $new_filepath = file_unmanaged_copy($file->filepath, dirname($file->filepath), FILE_EXISTS_RENAME);
+    $new_filepath = file_unmanaged_copy($file->uri, drupal_dirname($file->uri), FILE_EXISTS_RENAME);
     $this->assertTrue($new_filepath, t('Copying into same directory works.'));
-    $this->assertNotEqual($new_filepath, $file->filepath, t('Copied file has a new name.'));
-    $this->assertTrue(file_exists($file->filepath), t('Original file exists after copying onto itself.'));
+    $this->assertNotEqual($new_filepath, $file->uri, t('Copied file has a new name.'));
+    $this->assertTrue(file_exists($file->uri), t('Original file exists after copying onto itself.'));
     $this->assertTrue(file_exists($new_filepath), t('Copied file exists after copying onto itself.'));
     $this->assertFilePermissions($new_filepath, variable_get('file_chmod_file', 0664));
   }
@@ -1254,10 +1220,10 @@ class FileDeleteTest extends FileHookTestCase {
     $file = $this->createFile();
 
     // Check that deletion removes the file and database record.
-    $this->assertTrue(is_file($file->filepath), t("File exists."));
+    $this->assertTrue(is_file($file->uri), t("File exists."));
     $this->assertIdentical(file_delete($file), TRUE, t("Delete worked."));
     $this->assertFileHooksCalled(array('references', 'delete'));
-    $this->assertFalse(file_exists($file->filepath), t("Test file has actually been deleted."));
+    $this->assertFalse(file_exists($file->uri), t("Test file has actually been deleted."));
     $this->assertFalse(file_load($file->fid), t('File was removed from the database.'));
 
     // TODO: implement hook_file_references() in file_test.module and report a
@@ -1284,7 +1250,7 @@ class FileMoveTest extends FileHookTestCase {
   function testNormal() {
     $contents = $this->randomName(10);
     $source = $this->createFile(NULL, $contents);
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
+    $desired_filepath = 'public://' . $this->randomName();
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
@@ -1292,8 +1258,8 @@ class FileMoveTest extends FileHookTestCase {
 
     // Check the return status and that the contents changed.
     $this->assertTrue($result, t('File moved sucessfully.'));
-    $this->assertFalse(file_exists($source->filepath));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file correctly written.'));
+    $this->assertFalse(file_exists($source->uri));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file correctly written.'));
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('move', 'update'));
@@ -1320,12 +1286,12 @@ class FileMoveTest extends FileHookTestCase {
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_move(clone $source, $target->filepath, FILE_EXISTS_RENAME);
+    $result = file_move(clone $source, $target->uri, FILE_EXISTS_RENAME);
 
     // Check the return status and that the contents changed.
     $this->assertTrue($result, t('File moved sucessfully.'));
-    $this->assertFalse(file_exists($source->filepath));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file correctly written.'));
+    $this->assertFalse(file_exists($source->uri));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file correctly written.'));
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('move', 'update'));
@@ -1340,7 +1306,7 @@ class FileMoveTest extends FileHookTestCase {
     // Compare the source and results.
     $loaded_source = file_load($source->fid, TRUE);
     $this->assertEqual($loaded_source->fid, $result->fid, t("Returned file's id matches the source."));
-    $this->assertNotEqual($loaded_source->filepath, $source->filepath, t("Returned file path has changed from the original."));
+    $this->assertNotEqual($loaded_source->uri, $source->uri, t("Returned file path has changed from the original."));
   }
 
   /**
@@ -1355,11 +1321,11 @@ class FileMoveTest extends FileHookTestCase {
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_move(clone $source, $target->filepath, FILE_EXISTS_REPLACE);
+    $result = file_move(clone $source, $target->uri, FILE_EXISTS_REPLACE);
 
     // Look at the results.
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file were overwritten.'));
-    $this->assertFalse(file_exists($source->filepath));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file were overwritten.'));
+    $this->assertFalse(file_exists($source->uri));
     $this->assertTrue($result, t('File moved sucessfully.'));
 
     // Check that the correct hooks were called.
@@ -1385,9 +1351,9 @@ class FileMoveTest extends FileHookTestCase {
 
     // Copy the file over itself. Clone the object so we don't have to worry
     // about the function changing our reference copy.
-    $result = file_move(clone $source, $source->filepath, FILE_EXISTS_REPLACE);
+    $result = file_move(clone $source, $source->uri, FILE_EXISTS_REPLACE);
     $this->assertFalse($result, t('File move failed.'));
-    $this->assertEqual($contents, file_get_contents($source->filepath), t('Contents of file were not altered.'));
+    $this->assertEqual($contents, file_get_contents($source->uri), t('Contents of file were not altered.'));
 
     // Check that no hooks were called while failing.
     $this->assertFileHooksCalled(array());
@@ -1409,12 +1375,12 @@ class FileMoveTest extends FileHookTestCase {
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_move(clone $source, $target->filepath, FILE_EXISTS_ERROR);
+    $result = file_move(clone $source, $target->uri, FILE_EXISTS_ERROR);
 
     // Check the return status and that the contents did not change.
     $this->assertFalse($result, t('File move failed.'));
-    $this->assertTrue(file_exists($source->filepath));
-    $this->assertEqual($contents, file_get_contents($target->filepath), t('Contents of file were not altered.'));
+    $this->assertTrue(file_exists($source->uri));
+    $this->assertEqual($contents, file_get_contents($target->uri), t('Contents of file were not altered.'));
 
     // Check that no hooks were called while failing.
     $this->assertFileHooksCalled(array());
@@ -1443,25 +1409,25 @@ class FileCopyTest extends FileHookTestCase {
    * Test file copying in the normal, base case.
    */
   function testNormal() {
-    $contents = $this->randomName(10);
-    $source = $this->createFile(NULL, $contents);
-    $desired_filepath = file_directory_path() . '/' . $this->randomName();
+    $contents    = $this->randomName(10);
+    $source      = $this->createFile(NULL, $contents);
+    $desired_uri = 'public://' . $this->randomName();
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_copy(clone $source, $desired_filepath, FILE_EXISTS_ERROR);
+    $result = file_copy(clone $source, $desired_uri, FILE_EXISTS_ERROR);
 
     // Check the return status and that the contents changed.
     $this->assertTrue($result, t('File copied sucessfully.'));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file were copied correctly.'));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file were copied correctly.'));
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('copy', 'insert'));
 
     $this->assertDifferentFile($source, $result);
-    $this->assertEqual($result->filepath, $desired_filepath, t('The copied file object has the desired filepath.'));
-    $this->assertTrue(file_exists($source->filepath), t('The original file still exists.'));
-    $this->assertTrue(file_exists($result->filepath), t('The copied file exists.'));
+    $this->assertEqual($result->uri, $desired_uri, t('The copied file object has the desired filepath.'));
+    $this->assertTrue(file_exists($source->uri), t('The original file still exists.'));
+    $this->assertTrue(file_exists($result->uri), t('The copied file exists.'));
 
     // Reload the file from the database and check that the changes were
     // actually saved.
@@ -1474,18 +1440,18 @@ class FileCopyTest extends FileHookTestCase {
   function testExistingRename() {
     // Setup a file to overwrite.
     $contents = $this->randomName(10);
-    $source = $this->createFile(NULL, $contents);
-    $target = $this->createFile();
+    $source   = $this->createFile(NULL, $contents);
+    $target   = $this->createFile();
     $this->assertDifferentFile($source, $target);
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_copy(clone $source, $target->filepath, FILE_EXISTS_RENAME);
+    $result = file_copy(clone $source, $target->uri, FILE_EXISTS_RENAME);
 
     // Check the return status and that the contents changed.
     $this->assertTrue($result, t('File copied sucessfully.'));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file were copied correctly.'));
-    $this->assertNotEqual($result->filepath, $source->filepath, t('Returned file path has changed from the original.'));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file were copied correctly.'));
+    $this->assertNotEqual($result->uri, $source->uri, t('Returned file path has changed from the original.'));
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array('copy', 'insert'));
@@ -1520,11 +1486,11 @@ class FileCopyTest extends FileHookTestCase {
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_copy(clone $source, $target->filepath, FILE_EXISTS_REPLACE);
+    $result = file_copy(clone $source, $target->uri, FILE_EXISTS_REPLACE);
 
     // Check the return status and that the contents changed.
     $this->assertTrue($result, t('File copied sucessfully.'));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of file were overwritten.'));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of file were overwritten.'));
     $this->assertDifferentFile($source, $result);
 
     // Check that the correct hooks were called.
@@ -1558,11 +1524,11 @@ class FileCopyTest extends FileHookTestCase {
 
     // Clone the object so we don't have to worry about the function changing
     // our reference copy.
-    $result = file_copy(clone $source, $target->filepath, FILE_EXISTS_ERROR);
+    $result = file_copy(clone $source, $target->uri, FILE_EXISTS_ERROR);
 
     // Check the return status and that the contents were not changed.
     $this->assertFalse($result, t('File copy failed.'));
-    $this->assertEqual($contents, file_get_contents($target->filepath), t('Contents of file were not altered.'));
+    $this->assertEqual($contents, file_get_contents($target->uri), t('Contents of file were not altered.'));
 
     // Check that the correct hooks were called.
     $this->assertFileHooksCalled(array());
@@ -1594,10 +1560,10 @@ class FileLoadTest extends FileHookTestCase {
   }
 
   /**
-   * Try to load a non-existent file by filepath.
+   * Try to load a non-existent file by URI.
    */
   function testLoadMissingFilepath() {
-    $this->assertFalse(reset(file_load_multiple(array(), array('filepath' => 'misc/druplicon.png'))), t("Try to load a file that doesn't exist in the database fails."));
+    $this->assertFalse(reset(file_load_multiple(array(), array('uri' => 'foobar://misc/druplicon.png'))), t("Try to load a file that doesn't exist in the database fails."));
     $this->assertFileHooksCalled(array());
   }
 
@@ -1614,21 +1580,13 @@ class FileLoadTest extends FileHookTestCase {
    */
   function testSingleValues() {
     // Create a new file object from scratch so we know the values.
-    $file = array(
-      'uid' => 1,
-      'filename' => 'druplicon.png',
-      'filepath' => 'misc/druplicon.png',
-      'filemime' => 'image/png',
-      'timestamp' => 1,
-      'status' => FILE_STATUS_PERMANENT,
-    );
-    $file = file_save($file);
+    $file = $this->createFile('druplicon.txt', NULL, 'public://');
 
     $by_fid_file = file_load($file->fid);
     $this->assertFileHookCalled('load');
     $this->assertTrue(is_object($by_fid_file), t('file_load() returned an object.'));
     $this->assertEqual($by_fid_file->fid, $file->fid, t("Loading by fid got the same fid."), 'File');
-    $this->assertEqual($by_fid_file->filepath, $file->filepath, t("Loading by fid got the correct filepath."), 'File');
+    $this->assertEqual($by_fid_file->uri, $file->uri, t("Loading by fid got the correct filepath."), 'File');
     $this->assertEqual($by_fid_file->filename, $file->filename, t("Loading by fid got the correct filename."), 'File');
     $this->assertEqual($by_fid_file->filemime, $file->filemime, t("Loading by fid got the correct MIME type."), 'File');
     $this->assertEqual($by_fid_file->status, $file->status, t("Loading by fid got the correct status."), 'File');
@@ -1640,19 +1598,11 @@ class FileLoadTest extends FileHookTestCase {
    */
   function testMultiple() {
     // Create a new file object.
-    $file = array(
-      'uid' => 1,
-      'filename' => 'druplicon.png',
-      'filepath' => 'misc/druplicon.png',
-      'filemime' => 'image/png',
-      'timestamp' => 1,
-      'status' => FILE_STATUS_PERMANENT,
-    );
-    $file = file_save($file);
+    $file = $this->createFile('druplicon.txt', NULL, 'public://');
 
     // Load by path.
     file_test_reset();
-    $by_path_files = file_load_multiple(array(), array('filepath' => $file->filepath));
+    $by_path_files = file_load_multiple(array(), array('uri' => $file->uri));
     $this->assertFileHookCalled('load');
     $this->assertEqual(1, count($by_path_files), t('file_load_multiple() returned an array of the correct size.'));
     $by_path_file = reset($by_path_files);
@@ -1666,7 +1616,7 @@ class FileLoadTest extends FileHookTestCase {
     $this->assertEqual(1, count($by_fid_files), t('file_load_multiple() returned an array of the correct size.'));
     $by_fid_file = reset($by_fid_files);
     $this->assertTrue($by_fid_file->file_test['loaded'], t('file_test_file_load() was able to modify the file during load.'));
-    $this->assertEqual($by_fid_file->filepath, $file->filepath, t("Loading by fid got the correct filepath."), 'File');
+    $this->assertEqual($by_fid_file->uri, $file->uri, t("Loading by fid got the correct filepath."), 'File');
   }
 }
 
@@ -1686,13 +1636,15 @@ class FileSaveTest extends FileHookTestCase {
     // Create a new file object.
     $file = array(
       'uid' => 1,
-      'filename' => 'druplicon.png',
-      'filepath' => 'misc/druplicon.png',
-      'filemime' => 'image/png',
+      'filename'  => 'druplicon.txt',
+      'uri'       => 'public://druplicon.txt',
+      'filemime'  => 'text/plain',
       'timestamp' => 1,
-      'status' => FILE_STATUS_PERMANENT,
+      'status'    => FILE_STATUS_PERMANENT,
     );
-    $file = (object) $file;
+    $file = (object)$file;
+
+    file_put_contents($file->uri, 'hello world');
 
     // Save it, inserting a new record.
     $saved_file = file_save($file);
@@ -1702,10 +1654,10 @@ class FileSaveTest extends FileHookTestCase {
 
     $this->assertNotNull($saved_file, t("Saving the file should give us back a file object."), 'File');
     $this->assertTrue($saved_file->fid > 0, t("A new file ID is set when saving a new file to the database."), 'File');
-    $loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
+    $loaded_file = db_query('SELECT * FROM {file} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
     $this->assertNotNull($loaded_file, t("Record exists in the database."));
     $this->assertEqual($loaded_file->status, $file->status, t("Status was saved correctly."));
-    $this->assertEqual($saved_file->filesize, filesize($file->filepath), t("File size was set correctly."), 'File');
+    $this->assertEqual($saved_file->filesize, filesize($file->uri), t("File size was set correctly."), 'File');
     $this->assertTrue($saved_file->timestamp > 1, t("File size was set correctly."), 'File');
 
 
@@ -1719,7 +1671,7 @@ class FileSaveTest extends FileHookTestCase {
 
     $this->assertEqual($resaved_file->fid, $saved_file->fid, t("The file ID of an existing file is not changed when updating the database."), 'File');
     $this->assertTrue($resaved_file->timestamp >= $saved_file->timestamp, t("Timestamp didn't go backwards."), 'File');
-    $loaded_file = db_query('SELECT * FROM {files} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
+    $loaded_file = db_query('SELECT * FROM {file} f WHERE f.fid = :fid', array(':fid' => $saved_file->fid))->fetch(PDO::FETCH_OBJ);
     $this->assertNotNull($loaded_file, t("Record still exists in the database."), 'File');
     $this->assertEqual($loaded_file->status, $saved_file->status, t("Status was saved correctly."));
   }
@@ -1786,9 +1738,9 @@ class FileSaveDataTest extends FileHookTestCase {
     $result = file_save_data($contents);
     $this->assertTrue($result, t('Unnamed file saved correctly.'));
 
-    $this->assertEqual(file_directory_path(), dirname($result->filepath), t("File was placed in Drupal's files directory."));
-    $this->assertEqual($result->filename, basename($result->filepath), t("Filename was set to the file's basename."));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of the file are correct.'));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual($result->filename, basename($result->uri), t("Filename was set to the file's basename."));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'application/octet-stream', t('A MIME type was set.'));
     $this->assertEqual($result->status, FILE_STATUS_PERMANENT, t("The file's status was set to permanent."));
 
@@ -1805,12 +1757,12 @@ class FileSaveDataTest extends FileHookTestCase {
   function testWithFilename() {
     $contents = $this->randomName(8);
 
-    $result = file_save_data($contents, 'asdf.txt');
+    $result = file_save_data($contents, 'public://' . 'asdf.txt');
     $this->assertTrue($result, t('Unnamed file saved correctly.'));
 
-    $this->assertEqual(file_directory_path(), dirname($result->filepath), t("File was placed in Drupal's files directory."));
-    $this->assertEqual('asdf.txt', basename($result->filepath), t('File was named correctly.'));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of the file are correct.'));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
+    $this->assertEqual('asdf.txt', basename($result->uri), t('File was named correctly.'));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'text/plain', t('A MIME type was set.'));
     $this->assertEqual($result->status, FILE_STATUS_PERMANENT, t("The file's status was set to permanent."));
 
@@ -1829,12 +1781,12 @@ class FileSaveDataTest extends FileHookTestCase {
     $existing = $this->createFile();
     $contents = $this->randomName(8);
 
-    $result = file_save_data($contents, $existing->filepath, FILE_EXISTS_RENAME);
+    $result = file_save_data($contents, $existing->uri, FILE_EXISTS_RENAME);
     $this->assertTrue($result, t("File saved sucessfully."));
 
-    $this->assertEqual(file_directory_path(), dirname($result->filepath), t("File was placed in Drupal's files directory."));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
     $this->assertEqual($result->filename, $existing->filename, t("Filename was set to the basename of the source, rather than that of the renamed file."));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t("Contents of the file are correct."));
+    $this->assertEqual($contents, file_get_contents($result->uri), t("Contents of the file are correct."));
     $this->assertEqual($result->filemime, 'application/octet-stream', t("A MIME type was set."));
     $this->assertEqual($result->status, FILE_STATUS_PERMANENT, t("The file's status was set to permanent."));
 
@@ -1857,12 +1809,12 @@ class FileSaveDataTest extends FileHookTestCase {
     $existing = $this->createFile();
     $contents = $this->randomName(8);
 
-    $result = file_save_data($contents, $existing->filepath, FILE_EXISTS_REPLACE);
+    $result = file_save_data($contents, $existing->uri, FILE_EXISTS_REPLACE);
     $this->assertTrue($result, t('File saved sucessfully.'));
 
-    $this->assertEqual(file_directory_path(), dirname($result->filepath), t("File was placed in Drupal's files directory."));
+    $this->assertEqual(file_directory_path('public://'), file_directory_path(file_stream_wrapper_valid_scheme($result->uri)), t("File was placed in Drupal's files directory."));
     $this->assertEqual($result->filename, $existing->filename, t('Filename was set to the basename of the existing file, rather than preserving the original name.'));
-    $this->assertEqual($contents, file_get_contents($result->filepath), t('Contents of the file are correct.'));
+    $this->assertEqual($contents, file_get_contents($result->uri), t('Contents of the file are correct.'));
     $this->assertEqual($result->filemime, 'application/octet-stream', t('A MIME type was set.'));
     $this->assertEqual($result->status, FILE_STATUS_PERMANENT, t("The file's status was set to permanent."));
 
@@ -1884,9 +1836,9 @@ class FileSaveDataTest extends FileHookTestCase {
     $existing = $this->createFile(NULL, $contents);
 
     // Check the overwrite error.
-    $result = file_save_data('asdf', $existing->filepath, FILE_EXISTS_ERROR);
+    $result = file_save_data('asdf', $existing->uri, FILE_EXISTS_ERROR);
     $this->assertFalse($result, t('Overwriting a file fails when FILE_EXISTS_ERROR is specified.'));
-    $this->assertEqual($contents, file_get_contents($existing->filepath), t('Contents of existing file were unchanged.'));
+    $this->assertEqual($contents, file_get_contents($existing->uri), t('Contents of existing file were unchanged.'));
 
     // Check that no hooks were called while failing.
     $this->assertFileHooksCalled(array());
@@ -1917,11 +1869,10 @@ class FileDownloadTest extends FileTestCase {
    */
   function testPrivateFileTransfer() {
     // Set file downloads to private so handler functions get called.
-    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
 
     // Create a file.
-    $file = $this->createFile();
-    $url = file_create_url($file->filename);
+    $file = $this->createFile(NULL, NULL, 'private://');
+    $url  = file_external_url($file->uri);
 
     // Set file_test access header to allow the download.
     file_test_set_return('download', array('x-foo' => 'Bar'));
@@ -1936,7 +1887,7 @@ class FileDownloadTest extends FileTestCase {
     $this->assertResponse(403, t('Correctly denied access to a file when file_test sets the header to -1.'));
 
     // Try non-existent file.
-    $url = file_create_url($this->randomName());
+    $url = file_external_url('private://' . $this->randomName());
     $this->drupalHead($url);
     $this->assertResponse(404, t('Correctly returned 404 response for a non-existent file.'));
   }
@@ -2019,6 +1970,8 @@ class FileMimeTypeTest extends DrupalWebTestCase {
    * Test mapping of mimetypes from filenames.
    */
   public function testFileMimeTypeDetection() {
+    $prefix = 'simpletest://';
+
     $test_case = array(
       'test.jar' => 'application/java-archive',
       'test.jpeg' => 'image/jpeg',
@@ -2065,6 +2018,11 @@ class FileMimeTypeTest extends DrupalWebTestCase {
 
     variable_set('mime_extension_mapping', $mapping);
     foreach ($test_case as $input => $expected) {
+      // Test stream [URI].
+      $output = file_get_mimetype($prefix . $input);
+      $this->assertIdentical($output, $expected, t('Mimetype for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
+
+      // Test normal path equivalent
       $output = file_get_mimetype($input);
       $this->assertIdentical($output, $expected, t('Mimetype (using mappings from variable) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected)));
     }
@@ -2095,8 +2053,8 @@ class StreamWrapperRegistryTest extends DrupalWebTestCase {
   }
 
   function setUp() {
-    drupal_static_reset('file_get_stream_wrappers');
     parent::setUp('file_test');
+    drupal_static_reset('file_get_stream_wrappers');
   }
 
   function tearDown() {
@@ -2136,9 +2094,9 @@ class StreamWrapperRegistryTest extends DrupalWebTestCase {
     $this->assertEqual('DrupalPublicStreamWrapper', get_class($instance), t('Got correct class type for public URI.'));
 
     // Test file_stream_wrapper_uri_normalize.
-    $uri = 'public:///' . $this->originalFileDirectory . '/foo/bar/';
+    $uri = 'public:///' . file_directory_path() . '/foo/bar/';
     $uri = file_stream_wrapper_uri_normalize($uri);
-    $this->assertEqual('public://foo/bar', $uri, t('Got a properly normalized URI'));
+    $this->assertEqual('public://foo/bar', $uri, t('Got a properly normalized URI @uri', array('@uri' => $uri)));
 
     // Test file_uri_taget().
     $this->assertEqual('foo/bar.txt', file_uri_target('public://foo/bar.txt'), t('Got a valid stream target from public://foo/bar.txt'));
diff --git modules/simpletest/tests/file_test.module modules/simpletest/tests/file_test.module
index ae1e724..31553a5 100644
--- modules/simpletest/tests/file_test.module
+++ modules/simpletest/tests/file_test.module
@@ -70,7 +70,7 @@ function _file_test_form_submit(&$form, &$form_state) {
   $file = file_save_upload('file_test_upload', array('file_validate_is_image' => array()), FALSE, $form_state['values']['file_test_replace']);
   if ($file) {
     $form_state['values']['file_test_upload'] = $file;
-    drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->filepath)));
+    drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->uri)));
     drupal_set_message(t('You WIN!'));
   }
   elseif ($file === FALSE) {
diff --git modules/simpletest/tests/filetransfer.test modules/simpletest/tests/filetransfer.test
index 2f4db1f..1cd2168 100644
--- modules/simpletest/tests/filetransfer.test
+++ modules/simpletest/tests/filetransfer.test
@@ -36,7 +36,7 @@ class FileTranferTest extends DrupalWebTestCase {
   }
 
   function _buildFakeModule() {
-    $location = file_directory_temp() . '/fake';
+    $location = file_directory_path('temporary') . '/fake';
     if (is_dir($location)) {
       $ret = 0;
       $output = array();
diff --git modules/simpletest/tests/image.test modules/simpletest/tests/image.test
index 91d3517..4931266 100644
--- modules/simpletest/tests/image.test
+++ modules/simpletest/tests/image.test
@@ -444,7 +444,7 @@ class ImageToolkitGdTestCase extends DrupalWebTestCase {
         }
 
         $directory = file_directory_path() . '/imagetests';
-        file_check_directory($directory, FILE_CREATE_DIRECTORY);
+        file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
         image_save($image, $directory . '/' . $op . '.' . $image->info['extension']);
 
         $this->assertTrue($correct_dimensions_real, t('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
diff --git modules/simpletest/tests/session.test modules/simpletest/tests/session.test
index 0f0e051..ee9bfb6 100644
--- modules/simpletest/tests/session.test
+++ modules/simpletest/tests/session.test
@@ -219,7 +219,7 @@ class SessionTestCase extends DrupalWebTestCase {
     $this->loggedInUser = FALSE;
 
     // Change cookie file for user.
-    $this->cookieFile = file_directory_temp() . '/cookie.' . $uid . '.txt';
+    $this->cookieFile = file_directory_path('temporary') . '/cookie.' . $uid . '.txt';
     $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
     $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
     $this->drupalGet('session-test/get');
diff --git modules/system/image.gd.inc modules/system/image.gd.inc
index 91f5296..29393d2 100644
--- modules/system/image.gd.inc
+++ modules/system/image.gd.inc
@@ -254,6 +254,11 @@ function image_gd_load(stdClass $image) {
  * @see image_save()
  */
 function image_gd_save(stdClass $image, $destination) {
+  // Convert URI to a normal path because PHP apparently has some gaps in stream wrapper support.
+  if ($wrapper = file_stream_wrapper_get_instance_by_uri($destination)) {
+    $destination = $wrapper->realpath();
+  }
+  
   $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
   $function = 'image' . $extension;
   if (!function_exists($function)) {
diff --git modules/system/system.admin.inc modules/system/system.admin.inc
index 80f1a9e..af9c590 100644
--- modules/system/system.admin.inc
+++ modules/system/system.admin.inc
@@ -366,7 +366,9 @@ function system_themes_form_submit($form, &$form_state) {
  */
 function system_theme_settings(&$form_state, $key = '') {
   $directory_path = file_directory_path();
-  file_check_directory($directory_path, FILE_CREATE_DIRECTORY, 'file_directory_path');
+  if (!file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY)) {
+    drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directory_path)), 'warning');
+  }
 
   // Default settings are defined in theme_get_settings() in includes/theme.inc
   if ($key) {
@@ -390,7 +392,7 @@ function system_theme_settings(&$form_state, $key = '') {
     // The image was saved using file_save_upload() and was added to the
     // files table as a temporary file. We'll make a copy and let the garbage
     // collector delete the original upload.
-    if ($filepath = file_unmanaged_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) {
+    if ($filepath = file_unmanaged_copy($file->uri, $filename, FILE_EXISTS_REPLACE)) {
       $_POST['default_logo'] = 0;
       $_POST['logo_path'] = $filepath;
       $_POST['toggle_logo'] = 1;
@@ -405,7 +407,7 @@ function system_theme_settings(&$form_state, $key = '') {
     // The image was saved using file_save_upload() and was added to the
     // files table as a temporary file. We'll make a copy and let the garbage
     // collector delete the original upload.
-    if ($filepath = file_unmanaged_copy($file->filepath, $filename, FILE_EXISTS_REPLACE)) {
+    if ($filepath = file_unmanaged_copy($file->uri, $filename, FILE_EXISTS_REPLACE)) {
       $_POST['default_favicon'] = 0;
       $_POST['favicon_path'] = $filepath;
       $_POST['toggle_favicon'] = 1;
@@ -1409,20 +1411,14 @@ function system_performance_settings() {
     '#description' => t('The minimum amount of time that will elapse before the caches are recreated.')
   );
 
-  $directory = file_directory_path();
+  $directory = 'public://';
   $is_writable = is_dir($directory) && is_writable($directory);
-  $public_downloads = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
-  $disabled = !$is_writable && !$public_downloads;
+  $disabled = !$is_writable;
   $disabled_message = '';
-  if(!$is_writable && !$public_downloads) {
-    $disabled_message = ' ' . t('<strong class="error">Please <a href="!file-system">set up</a> the files directory and set the download method to public to make these optimizations available.</strong>', array('!file-system' => url('admin/settings/file-system')));
-  }
-  elseif(!$is_writable) {
-    $disabled_message = ' ' . t('<strong class="error">Please set up the <a href="!file-system">files directory</a> to make these optimizations available.</strong>', array('!file-system' => url('admin/settings/file-system')));
-  }
-  elseif(!$public_downloads) {
-    $disabled_message = ' ' . t('<strong class="error">Please set the <a href="!file-system">download method</a> to public to make these optimizations available.</strong>', array('!file-system' => url('admin/settings/file-system')));
+  if(!$is_writable) {
+    $disabled_message = ' ' . t('<strong class="error">Please set up the <a href="!file-system">public files directory</a> to make these optimizations available.</strong>', array('!file-system' => url('admin/settings/file-system')));
   }
+
   $form['bandwidth_optimization'] = array(
     '#type' => 'fieldset',
     '#title' => t('Bandwidth optimization'),
@@ -1477,31 +1473,45 @@ function system_clear_cache_submit($form, &$form_state) {
  */
 function system_file_system_settings() {
 
-  $form['file_directory_path'] = array(
+  $form['file_public_path'] = array(
     '#type' => 'textfield',
-    '#title' => t('File system path'),
+    '#title' => t('Public file system path'),
     '#default_value' => file_directory_path(),
     '#maxlength' => 255,
-    '#description' => t('A file system path where the files will be stored. This directory must exist and be writable by Drupal. If the download method is set to public, this directory must be relative to the Drupal installation directory and be accessible over the web. If the download method is set to private, this directory should not be accessible over the web. Changing this location will modify all download paths and may cause unexpected problems on an existing site.'),
+    '#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web.'),
     '#after_build' => array('system_check_directory'),
   );
 
-  $form['file_directory_temp'] = array(
+  $form['file_private_path'] = array(
     '#type' => 'textfield',
-    '#title' => t('Temporary directory'),
-    '#default_value' => file_directory_temp(),
+    '#title' => t('Private file system path'),
+    '#default_value' => file_directory_path('private'),
     '#maxlength' => 255,
-    '#description' => t('A file system path where uploaded files will be stored during previews.'),
+    '#description' => t('A local file system path where private files will be stored. This directory must exist and be writable by Drupal. This directory should not be accessible over the web.'),
     '#after_build' => array('system_check_directory'),
   );
 
-  $form['file_downloads'] = array(
+  $form['file_temporary_path'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Temporary directory'),
+    '#default_value' => file_directory_path('temporary'),
+    '#maxlength' => 255,
+    '#description' => t('A local file system path where temporary files will be stored. This directory should not be accessible over the web.'),
+    '#after_build' => array('system_check_directory'),
+  );
+  $wrappers = file_get_stream_wrappers();
+  $options = array(
+    'public' => $wrappers['public']['description'],
+    'private' => $wrappers['private']['description']
+  );
+  $form['file_default_scheme'] = array(
     '#type' => 'radios',
-    '#title' => t('Download method'),
-    '#default_value' => FILE_DOWNLOADS_PUBLIC,
-    '#options' => array(FILE_DOWNLOADS_PUBLIC => t('Public - files are available using HTTP directly.'), FILE_DOWNLOADS_PRIVATE => t('Private - files are transferred by Drupal.')),
-    '#description' => t('Choose the <em>Public download</em> method unless you wish to enforce fine-grained access controls over file downloads. Changing the download method will modify all download paths and may cause unexpected problems on an existing site.')
+    '#title' => t('Default download method'),
+    '#default_value' => 'public',
+    '#options' => $options,
+    '#description' => t('This setting is used as the preferred download method. The use of public files is more efficient, but does not provide any access control.'),
   );
+  
 
   return system_settings_form($form, TRUE);
 }
diff --git modules/system/system.api.php modules/system/system.api.php
index 1459b45..9fbe401 100644
--- modules/system/system.api.php
+++ modules/system/system.api.php
@@ -1316,7 +1316,8 @@ function hook_file_delete($file) {
 function hook_file_download($filepath) {
   // Check if the file is controlled by the current module.
   $filepath = file_create_path($filepath);
-  $result = db_query("SELECT f.* FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :filepath", array('filepath' => $filepath));
+  // TODO: This will need to be updated because of wrappers.
+  $result = db_query("SELECT f.* FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid WHERE uri = :uri", array('uri' => $filepath));
   foreach ($result as $file) {
     if (!user_access('view uploaded files')) {
       return -1;
diff --git modules/system/system.install modules/system/system.install
index f06217a..fadc917 100644
--- modules/system/system.install
+++ modules/system/system.install
@@ -176,49 +176,55 @@ function system_requirements($phase) {
     );
   }
 
-  // Test files directory
-  $directory = file_directory_path();
+  // Test files directories.
+  $directories = array(
+    variable_get('file_public_path', conf_path() . '/files'),
+    variable_get('file_private_path', conf_path() . '/private/files'),
+    variable_get('file_temporary_path', conf_path() . '/private/temp'),
+  );
   $requirements['file system'] = array(
     'title' => $t('File system'),
   );
 
-  // For installer, create the directory if possible.
-  if ($phase == 'install' && !is_dir($directory) && @mkdir($directory)) {
-    @chmod($directory, 0775); // Necessary for non-webserver users.
-  }
-
-  $is_writable = is_writable($directory);
-  $is_directory = is_dir($directory);
-  if (!$is_writable || !$is_directory) {
-    $description = '';
-    $requirements['file system']['value'] = $t('Not writable');
-    if (!$is_directory) {
-      $error = $t('The directory %directory does not exist.', array('%directory' => $directory));
-    }
-    else {
-      $error = $t('The directory %directory is not writable.', array('%directory' => $directory));
-    }
-    // The files directory requirement check is done only during install and runtime.
-    if ($phase == 'runtime') {
-      $description = $error . ' ' . $t('You may need to set the correct directory at the <a href="@admin-file-system">file system settings page</a> or change the current directory\'s permissions so that it is writable.', array('@admin-file-system' => url('admin/settings/file-system')));
-    }
-    elseif ($phase == 'install') {
-      // For the installer UI, we need different wording. 'value' will
-      // be treated as version, so provide none there.
-      $description = $error . ' ' . $t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually, or ensure that the installer has the permissions to create it automatically. For more information, please see INSTALL.txt or the <a href="@handbook_url">online handbook</a>.', array('@handbook_url' => 'http://drupal.org/server-permissions'));
-      $requirements['file system']['value'] = '';
-    }
-    if (!empty($description)) {
-      $requirements['file system']['description'] = $description;
-      $requirements['file system']['severity'] = REQUIREMENT_ERROR;
+  $error = '';
+  // For installer, create the directories if possible.
+  foreach ($directories as $directory) {
+    if ($phase == 'install') {
+      file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
     }
-  }
-  else {
-    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
-      $requirements['file system']['value'] = $t('Writable (<em>public</em> download method)');
+    $is_writable = is_writable($directory);
+    $is_directory = is_dir($directory);
+    if (!$is_writable || !$is_directory) {
+      $description = '';
+      $requirements['file system']['value'] = $t('Not writable');
+      if (!$is_directory) {
+        $error .= $t('The directory %directory does not exist.', array('%directory' => $directory)) . ' ';
+      }
+      else {
+        $error .= $t('The directory %directory is not writable.', array('%directory' => $directory)) . ' ';
+      }
+      // The files directory requirement check is done only during install and runtime.
+      if ($phase == 'runtime') {
+        $description = $error . $t('You may need to set the correct directory at the <a href="@admin-file-system">file system settings page</a> or change the current directory\'s permissions so that it is writable.', array('@admin-file-system' => url('admin/settings/file-system')));
+      }
+      elseif ($phase == 'install') {
+        // For the installer UI, we need different wording. 'value' will
+        // be treated as version, so provide none there.
+        $description = $error . $t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually, or ensure that the installer has the permissions to create it automatically. For more information, please see INSTALL.txt or the <a href="@handbook_url">online handbook</a>.', array('@handbook_url' => 'http://drupal.org/server-permissions'));
+        $requirements['file system']['value'] = '';
+      }
+      if (!empty($description)) {
+        $requirements['file system']['description'] = $description;
+        $requirements['file system']['severity'] = REQUIREMENT_ERROR;
+      }
     }
     else {
-      $requirements['file system']['value'] = $t('Writable (<em>private</em> download method)');
+      if (variable_get('file_default_scheme', 'public') == 'public') {
+        $requirements['file system']['value'] = $t('Writable (<em>public</em> download method)');
+      }
+      else {
+        $requirements['file system']['value'] = $t('Writable (<em>private</em> download method)');
+      }
     }
   }
 
@@ -675,7 +681,7 @@ function system_schema() {
   $schema['cache_registry'] = $schema['cache'];
   $schema['cache_registry']['description'] = 'Cache table for the code registry system to remember what code files need to be loaded on any given page.';
 
-  $schema['files'] = array(
+  $schema['file'] = array(
     'description' => 'Stores information for uploaded files.',
     'fields' => array(
       'fid' => array(
@@ -698,7 +704,7 @@ function system_schema() {
         'not null' => TRUE,
         'default' => '',
       ),
-      'filepath' => array(
+      'uri' => array(
         'description' => 'Path of the file relative to Drupal root.',
         'type' => 'varchar',
         'length' => 255,
@@ -738,6 +744,9 @@ function system_schema() {
       'status' => array('status'),
       'timestamp' => array('timestamp'),
     ),
+    'unique keys' => array(
+      'uri' => array('uri'),
+    ),
     'primary key' => array('fid'),
     'foreign keys' => array(
       'uid' => array('users' => 'uid'),
@@ -2245,6 +2254,126 @@ function system_update_7032() {
 }
 
 /**
+* Migrate the file_downloads setting and create the new {file} table.
+ */
+function system_update_7033() {
+  $ret = array();
+  $files_directory = variable_get('file_directory_path', NULL);
+  if (variable_get('file_downloads', 1) == 1) {
+    // Our defualt is public, so we don't need to set anything.
+    if (!empty($files_directory)) {
+      variable_set('file_public_path', $files_directory);
+    }
+  }
+  elseif (variable_get('file_downloads', 1) == 2) {
+    variable_set('file_default_scheme', 'private');
+    if (!empty($files_directory)) {
+      variable_set('file_private_path', $files_directory);
+    }
+  }
+  variable_del('file_downloads');
+
+  $schema['file'] = array(
+    'description' => 'Stores information for uploaded files.',
+    'fields' => array(
+      'fid' => array(
+        'description' => 'File ID.',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'uid' => array(
+        'description' => 'The {user}.uid of the user who is associated with the file.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'filename' => array(
+        'description' => 'Name of the file with no path components. This may differ from the basename of the filepath if the file is renamed to avoid overwriting an existing file.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'uri' => array(
+        'description' => 'URI of file.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'filemime' => array(
+        'description' => "The file's MIME type.",
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'filesize' => array(
+        'description' => 'The size of the file in bytes.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'status' => array(
+        'description' => 'A bitmapped field indicating the status of the file the least sigifigant bit indicates temporary (1) or permanent (0). Temporary files older than DRUPAL_MAXIMUM_TEMP_FILE_AGE will be removed during a cron run.',
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'timestamp' => array(
+        'description' => 'UNIX timestamp for when the file was added.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'indexes' => array(
+      'uid' => array('uid'),
+      'status' => array('status'),
+      'timestamp' => array('timestamp'),
+    ),
+    'unique keys' => array(
+      'uri' => array('uri'),
+    ),
+    'primary key' => array('fid'),
+  );
+  db_create_table($ret, 'file', $schema['file']);
+  return $ret;
+}
+
+/**
+* Migrate upload module files to the new {file} table.
+ */
+function system_update_7034() {
+  $ret = array();
+  if (!db_table_exists('upload')) {
+    return $ret;
+  }
+  // The old {files} tables still exists.  We migrate core data from upload
+  // module, but any contrib module using it will need to do its own update.
+  $result = db_query('SELECT fid, uid, filename, filepath AS uri, filemime, filesize, status, timestamp FROM {files} f INNER JOIN {upload} u ON u.fid = f.fid', array(), array('fetch' => PDO::FETCH_ASSOC));
+
+  // We will convert filepaths to uri using the default schmeme
+  // and stripping off the existing file directory path.
+  $basename = variable_get('file_directory_path', conf_path() . '/files');
+  $scheme = variable_get('file_default_scheme', 'public') . '://';
+  $fids = array();
+  // TODO: does this function need to run in batch mode?
+  foreach ($result as $file) {
+    $file['uri'] = $scheme . str_replace($basename, '', $file['uri']);
+    $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
+    db_insert('file')->fields($file)->execute();
+    $fids[] = $file['fid'];
+  }
+  // TODO: delete the found fids from {files}?
+  return $ret;
+}
+
+/**
  * @} End of "defgroup updates-6.x-to-7.x"
  * The next series of updates should start at 8000.
  */
diff --git modules/system/system.module modules/system/system.module
index 656d16f..3ef6d75 100644
--- modules/system/system.module
+++ modules/system/system.module
@@ -473,6 +473,14 @@ function system_menu() {
   $items['system/files'] = array(
     'title' => 'File download',
     'page callback' => 'file_download',
+    'page arguments' => array('private'),
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
+  $items['system/temporary'] = array(
+    'title' => 'Temporary files',
+    'page callback' => 'file_download',
+    'page arguments' => array('temporary'),
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
@@ -1609,7 +1617,30 @@ function system_admin_menu_block($item) {
  *   The form element containing the name of the directory to check.
  */
 function system_check_directory($form_element) {
-  file_check_directory($form_element['#value'], FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
+  $directory = $form_element['#value'];
+  
+  if (!is_dir($directory) && !drupal_mkdir($directory, NULL, TRUE)) {
+    // If the directory does not exists and cannot be created.
+    form_set_error($form_element['#parents'][0], t('The directory %directory does not exist and could not be created.', array('%directory' => $directory)));
+    watchdog('file system', 'The directory %directory does not exist and could not be created.', array('%directory' => $directory), WATCHDOG_ERROR);
+  }
+
+  if (is_dir($directory) && !is_writable($directory) && !drupal_chmod($directory)) {
+    // If the directory is not writable and cannont be made so.
+    form_set_error($form_element['#parents'][0], t('The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory)));
+    watchdog('file system', 'The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory), WATCHDOG_ERROR);
+  }
+  else {
+    if ($form_element['#name'] == 'file_public_path') {
+      // Create public .htaccess file.
+      file_create_htaccess($directory, FALSE);
+    }
+    else {
+      // Create private .htaccess file.
+      file_create_htaccess($directory);
+    }
+  }
+
   return $form_element;
 }
 
@@ -1626,7 +1657,7 @@ function system_get_files_database(&$files, $type) {
   $result = db_query("SELECT filename, name, type, status, schema_version, weight FROM {system} WHERE type = :type", array(':type' => $type));
   foreach ($result as $file) {
     if (isset($files[$file->name]) && is_object($files[$file->name])) {
-      $file->filepath = $file->filename;
+      $file->uri = $file->filename;
       foreach ($file as $key => $value) {
         if (!isset($files[$file->name]->key)) {
           $files[$file->name]->$key = $value;
@@ -1702,7 +1733,7 @@ function system_update_files_database(&$files, $type) {
     }
     else {
       $query->values(array(
-        'filename' => $file->filepath,
+        'filename' => $file->uri,
         'name' => $file->name,
         'type' => $type,
         'owner' => isset($file->owner) ? $file->owner : '',
@@ -1740,7 +1771,7 @@ function _system_get_module_data() {
   // Read info files for each module.
   foreach ($modules as $key => $module) {
     // Look for the info file.
-    $module->info = drupal_parse_info_file(dirname($module->filepath) . '/' . $module->name . '.info');
+    $module->info = drupal_parse_info_file(dirname($module->uri) . '/' . $module->name . '.info');
 
     // Skip modules that don't provide info.
     if (empty($module->info)) {
@@ -1822,8 +1853,8 @@ function _system_get_theme_data() {
     $sub_themes = array();
     // Read info files for each theme
     foreach ($themes as $key => $theme) {
-      $themes[$key]->filename = $theme->filepath;
-      $themes[$key]->info = drupal_parse_info_file($theme->filepath) + $defaults;
+      $themes[$key]->filename = $theme->uri;
+      $themes[$key]->info = drupal_parse_info_file($theme->uri) + $defaults;
 
       // Invoke hook_system_info_alter() to give installed modules a chance to
       // modify the data in the .info files if necessary.
@@ -1833,7 +1864,7 @@ function _system_get_theme_data() {
         $sub_themes[] = $key;
       }
       if (empty($themes[$key]->info['engine'])) {
-        $filename = dirname($themes[$key]->filepath) . '/' . $themes[$key]->name . '.theme';
+        $filename = dirname($themes[$key]->uri) . '/' . $themes[$key]->name . '.theme';
         if (file_exists($filename)) {
           $themes[$key]->owner = $filename;
           $themes[$key]->prefix = $key;
@@ -1842,7 +1873,7 @@ function _system_get_theme_data() {
       else {
         $engine = $themes[$key]->info['engine'];
         if (isset($engines[$engine])) {
-          $themes[$key]->owner = $engines[$engine]->filepath;
+          $themes[$key]->owner = $engines[$engine]->uri;
           $themes[$key]->prefix = $engines[$engine]->name;
           $themes[$key]->template = TRUE;
         }
@@ -1853,7 +1884,7 @@ function _system_get_theme_data() {
       if (isset($themes[$key]->info['stylesheets'])) {
         foreach ($themes[$key]->info['stylesheets'] as $media => $stylesheets) {
           foreach ($stylesheets as $stylesheet) {
-            $pathed_stylesheets[$media][$stylesheet] = dirname($themes[$key]->filepath) . '/' . $stylesheet;
+            $pathed_stylesheets[$media][$stylesheet] = dirname($themes[$key]->uri) . '/' . $stylesheet;
           }
         }
       }
@@ -1863,13 +1894,13 @@ function _system_get_theme_data() {
       $scripts = array();
       if (isset($themes[$key]->info['scripts'])) {
         foreach ($themes[$key]->info['scripts'] as $script) {
-          $scripts[$script] = dirname($themes[$key]->filepath) . '/' . $script;
+          $scripts[$script] = dirname($themes[$key]->uri) . '/' . $script;
         }
       }
       $themes[$key]->info['scripts'] = $scripts;
       // Give the screenshot proper path information.
       if (!empty($themes[$key]->info['screenshot'])) {
-        $themes[$key]->info['screenshot'] = dirname($themes[$key]->filepath) . '/' . $themes[$key]->info['screenshot'];
+        $themes[$key]->info['screenshot'] = dirname($themes[$key]->uri) . '/' . $themes[$key]->info['screenshot'];
       }
     }
 
@@ -2238,7 +2269,7 @@ function system_cron() {
   // Remove temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
   // Use separate placeholders for the status to avoid a bug in some versions
   // of PHP. See http://drupal.org/node/352956
-  $result = db_query('SELECT fid FROM {files} WHERE status & :permanent1 <> :permanent2 AND timestamp < :timestamp', array(
+  $result = db_query('SELECT fid FROM {file} WHERE status & :permanent1 <> :permanent2 AND timestamp < :timestamp', array(
     ':permanent1' => FILE_STATUS_PERMANENT,
     ':permanent2' => FILE_STATUS_PERMANENT,
     ':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE
@@ -2246,7 +2277,7 @@ function system_cron() {
   foreach ($result as $row) {
     if ($file = file_load($row->fid)) {
       if (!file_delete($file)) {
-        watchdog('file system', 'Could not delete temporary file "%path" during garbage collection', array('%path' => $file->filepath), WATCHDOG_ERROR);
+        watchdog('file system', 'Could not delete temporary file "%path" during garbage collection', array('%path' => $file->uri), WATCHDOG_ERROR);
       }
     }
   }
@@ -3019,7 +3050,7 @@ function system_image_toolkits() {
  */
 function system_retrieve_file($url, $destination = NULL, $overwrite = TRUE) {
   if (!$destination) {
-    $destination = file_directory_temp();
+    $destination = file_directory_path('temporary');
   }
   $parsed_url = parse_url($url);
   $local = is_dir(file_directory_path() . '/' . $destination) ? $destination . '/' . basename($parsed_url['path']) : $destination;
diff --git modules/system/system.tar.inc modules/system/system.tar.inc
index a2ce6fe..236b938 100644
--- modules/system/system.tar.inc
+++ modules/system/system.tar.inc
@@ -1582,7 +1582,7 @@ class Archive_Tar
         if ($v_extract_file) {
           if ($v_header['typeflag'] == "5") {
             if (!@file_exists($v_header['filename'])) {
-                if (!@mkdir($v_header['filename'], 0777)) {
+                if (!@drupal_mkdir($v_header['filename'], 0777)) {
                     $this->_error('Unable to create directory {'
 					              .$v_header['filename'].'}');
                     return false;
@@ -1795,7 +1795,7 @@ class Archive_Tar
             (!$this->_dirCheck($p_parent_dir)))
              return false;
 
-        if (!@mkdir($p_dir, 0777)) {
+        if (!@drupal_mkdir($p_dir, 0777)) {
             $this->_error("Unable to create directory '$p_dir'");
             return false;
         }
diff --git modules/system/system.test modules/system/system.test
index 4b051c3..06d0913 100644
--- modules/system/system.test
+++ modules/system/system.test
@@ -400,41 +400,41 @@ class CronRunTestCase extends DrupalWebTestCase {
 
     // Temporary file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
     $temp_old = file_save_data('');
-    db_update('files')
+    db_update('file')
       ->fields(array(
         'status' => 0,
         'timestamp' => 1,
       ))
       ->condition('fid', $temp_old->fid)
       ->execute();
-    $this->assertTrue(file_exists($temp_old->filepath), t('Old temp file was created correctly.'));
+    $this->assertTrue(file_exists($temp_old->uri), t('Old temp file was created correctly.'));
 
     // Temporary file that is less than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
     $temp_new = file_save_data('');
-    db_update('files')
+    db_update('file')
       ->fields(array('status' => 0))
       ->condition('fid', $temp_new->fid)
       ->execute();
-    $this->assertTrue(file_exists($temp_new->filepath), t('New temp file was created correctly.'));
+    $this->assertTrue(file_exists($temp_new->uri), t('New temp file was created correctly.'));
 
     // Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
     $perm_old = file_save_data('');
-    db_update('files')
+    db_update('file')
       ->fields(array('timestamp' => 1))
       ->condition('fid', $temp_old->fid)
       ->execute();
-    $this->assertTrue(file_exists($perm_old->filepath), t('Old permanent file was created correctly.'));
+    $this->assertTrue(file_exists($perm_old->uri), t('Old permanent file was created correctly.'));
 
     // Permanent file that is newer than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
     $perm_new = file_save_data('');
-    $this->assertTrue(file_exists($perm_new->filepath), t('New permanent file was created correctly.'));
+    $this->assertTrue(file_exists($perm_new->uri), t('New permanent file was created correctly.'));
 
     // Run cron and then ensure that only the old, temp file was deleted.
     $this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
-    $this->assertFalse(file_exists($temp_old->filepath), t('Old temp file was correctly removed.'));
-    $this->assertTrue(file_exists($temp_new->filepath), t('New temp file was correctly ignored.'));
-    $this->assertTrue(file_exists($perm_old->filepath), t('Old permanent file was correctly ignored.'));
-    $this->assertTrue(file_exists($perm_new->filepath), t('New permanent file was correctly ignored.'));
+    $this->assertFalse(file_exists($temp_old->uri), t('Old temp file was correctly removed.'));
+    $this->assertTrue(file_exists($temp_new->uri), t('New temp file was correctly ignored.'));
+    $this->assertTrue(file_exists($perm_old->uri), t('Old permanent file was correctly ignored.'));
+    $this->assertTrue(file_exists($perm_new->uri), t('New permanent file was correctly ignored.'));
   }
 }
 
diff --git modules/update/update.compare.inc modules/update/update.compare.inc
index b49029f..9552724 100644
--- modules/update/update.compare.inc
+++ modules/update/update.compare.inc
@@ -80,7 +80,7 @@ function _update_process_info_list(&$projects, $list, $project_type) {
     // which is left alone by tar and correctly set to the time the .info file
     // was unpacked.
     if (!isset($file->info['_info_file_ctime'])) {
-      $info_filename = dirname($file->filepath) . '/' . $file->name . '.info';
+      $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
       $file->info['_info_file_ctime'] = filectime($info_filename);
     }
 
diff --git modules/upload/upload.install modules/upload/upload.install
index 0fa9d05..14ccd85 100644
--- modules/upload/upload.install
+++ modules/upload/upload.install
@@ -43,7 +43,7 @@ function upload_schema() {
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
-        'description' => 'Primary Key: The {files}.fid.',
+        'description' => 'Primary Key: The {file}.fid.',
       ),
       'nid' => array(
         'type' => 'int',
@@ -98,3 +98,58 @@ function upload_schema() {
 }
 
 
+/**
+ * Migrate upload module files from {files} to {file}.
+ */
+function upload_update_7000(&$sandbox) {
+  $ret = array();
+
+  /*
+    TODO: Fix the updates. This is broken. See http://drupal.org/node/329301#comment-1404336
+    Also note new DB structure http://drupal.org/node/227232#comment-1683976
+  */
+
+  if (!isset($sandbox['progress'])) {
+    // Initialize batch update information.
+    $sandbox['progress'] = 0;
+    $sandbox['last_fid_processed'] = -1;
+    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT u.fid) FROM {upload} u")->fetchField();
+  }
+
+  // As a batch operation move records from {files} into the {file} table.
+  $limit = 500;
+  $result = db_query_range("SELECT DISTINCT u.fid FROM {upload} u ORDER BY u.vid", array(), 0, $limit);
+  foreach ($result as $record) {
+    $old_file = db_query('SELECT f.* FROM {files} f WHERE f.fid = :fid', array(':fid' => $record->fid))->fetch(PDO::FETCH_OBJ);
+    if (!$old_file) {
+      continue;
+    }
+
+    $new_file = db_query('SELECT f.* FROM {files} f WHERE f.filepath = :filepath', array(':filepath' => $old_file->uri))->fetch(PDO::FETCH_OBJ);
+    if (!$new_file) {
+      // Re-save the file into the new {file} table.
+      $new_file = clone $old_file;
+      drupal_write_record('file', $new_file);
+    }
+
+    // If the fid has changed we need to update the {upload} record to use the
+    // new id.
+    if (!empty($new_file->fid) && ($new_file->fid != $old_file->fid)) {
+      db_update('upload')
+        ->fields(array('fid' => $new_file->fid))
+        ->condition('fid', $old_file->fid)
+        ->execute();
+    }
+
+    // Update our progress information for the batch update.
+    $sandbox['progress']++;
+    $sandbox['last_fid_processed'] = $old_file->fid;
+  }
+
+  // Indicate our current progress to the batch update system. If there's no
+  // max value then there's nothing to update and we're finished.
+  $ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
+
+  return $ret;
+}
+
diff --git modules/upload/upload.module modules/upload/upload.module
index 1381632..f4474f7 100644
--- modules/upload/upload.module
+++ modules/upload/upload.module
@@ -150,8 +150,8 @@ function _upload_file_limits($user) {
  * Implement hook_file_download().
  */
 function upload_file_download($filepath) {
-  $filepath = file_create_path($filepath);
-  $file = db_query("SELECT f.*, u.nid FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = :path", array(':path' => $filepath))->fetchObject();
+
+  $file = db_query("SELECT f.*, u.nid FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid WHERE uri = :path", array(':path' => $filepath))->fetchObject();
 
   if ($file && user_access('view uploaded files') && ($node = node_load($file->nid)) && node_access('view', $node)) {
     return array(
@@ -182,7 +182,7 @@ function upload_node_form_submit(&$form, &$form_state) {
   );
 
   // Save new file uploads.
-  if (user_access('upload files') && ($file = file_save_upload('upload', $validators, file_directory_path()))) {
+  if (user_access('upload files') && ($file = file_save_upload('upload', $validators, 'public://'))) {
     $file->list = variable_get('upload_list_default', 1);
     $file->description = $file->filename;
     $file->weight = 0;
@@ -245,9 +245,9 @@ function upload_form_alter(&$form, $form_state, $form_id) {
       // Make sure necessary directories for upload.module exist and are
       // writable before displaying the attachment form.
       $path = file_directory_path();
-      $temp = file_directory_temp();
+      $temp = file_directory_path('temporary');
       // Note: pass by reference
-      if (!file_check_directory($path, FILE_CREATE_DIRECTORY) || !file_check_directory($temp, FILE_CREATE_DIRECTORY)) {
+      if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY) || !file_prepare_directory($temp, FILE_CREATE_DIRECTORY)) {
         $form['attachments']['#description'] =  t('File attachments are disabled. The file directories have not been properly configured.');
         if (user_access('administer site configuration')) {
           $form['attachments']['#description'] .= ' ' . t('Please visit the <a href="@admin-file-system">file system configuration page</a>.', array('@admin-file-system' => url('admin/settings/file-system')));
@@ -372,7 +372,7 @@ function upload_node_view($node, $build_mode) {
       $node->rss_elements[] = array(
         'key' => 'enclosure',
         'attributes' => array(
-          'url' => file_create_url($file->filepath),
+          'url' => file_external_url($file->uri),
           'length' => $file->filesize,
           'type' => $file->filemime
         )
@@ -443,7 +443,7 @@ function theme_upload_attachments($elements) {
   foreach ($elements['#files'] as $file) {
     $file = (object)$file;
     if ($file->list && empty($file->remove)) {
-      $href = file_create_url($file->filepath);
+      $href = file_external_url($file->uri);
       $text = $file->description ? $file->description : $file->filename;
       $rows[] = array(l($text, $href), format_size($file->filesize));
     }
@@ -472,7 +472,7 @@ function upload_space_used($uid) {
  *   The amount of disk space used by uploaded files in bytes.
  */
 function upload_total_space_used() {
-  return db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid')->fetchField();
+  return db_query('SELECT SUM(f.filesize) FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid')->fetchField();
 }
 
 function upload_save($node) {
@@ -543,13 +543,13 @@ function _upload_form($node) {
       $file = (object)$file;
       $key = $file->fid;
 
-      $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => '<small>' . file_create_url($file->filepath) . '</small>');
+      $form['files'][$key]['description'] = array('#type' => 'textfield', '#default_value' => !empty($file->description) ? $file->description : $file->filename, '#maxlength' => 256, '#description' => '<small>' . file_external_url($file->uri) . '</small>');
       $form['files'][$key]['size'] = array('#markup' => format_size($file->filesize));
       $form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => !empty($file->remove));
       $form['files'][$key]['list'] = array('#type' => 'checkbox',  '#default_value' => $file->list);
       $form['files'][$key]['weight'] = array('#type' => 'weight', '#delta' => count($node->files), '#default_value' => $file->weight);
       $form['files'][$key]['filename'] = array('#type' => 'value',  '#value' => $file->filename);
-      $form['files'][$key]['filepath'] = array('#type' => 'value',  '#value' => $file->filepath);
+      $form['files'][$key]['uri'] = array('#type' => 'value',  '#value' => $file->uri);
       $form['files'][$key]['filemime'] = array('#type' => 'value',  '#value' => $file->filemime);
       $form['files'][$key]['filesize'] = array('#type' => 'value',  '#value' => $file->filesize);
       $form['files'][$key]['fid'] = array('#type' => 'value',  '#value' => $file->fid);
diff --git modules/upload/upload.test modules/upload/upload.test
index a13be4c..8d2b2d2 100644
--- modules/upload/upload.test
+++ modules/upload/upload.test
@@ -45,7 +45,7 @@ class UploadTestCase extends DrupalWebTestCase {
     // Create a node and attempt to attach files.
     $node = $this->drupalCreateNode();
     $text_files = $this->drupalGetTestFiles('text');
-    $files = array(current($text_files)->filepath, next($text_files)->filepath);
+    $files = array(current($text_files)->uri, next($text_files)->uri);
 
     $this->uploadFile($node, $files[0]);
     $this->uploadFile($node, $files[1]);
@@ -58,7 +58,7 @@ class UploadTestCase extends DrupalWebTestCase {
     $this->checkUploadedFile(basename($files[1]));
 
     // Check that files are also accessible when using private files.
-    variable_set('file_downloads', FILE_DOWNLOADS_PRIVATE);
+    variable_set('file_default_scheme', 'private');
     $this->checkUploadedFile(basename($files[0]));
     $this->checkUploadedFile(basename($files[1]));
 
@@ -86,7 +86,9 @@ class UploadTestCase extends DrupalWebTestCase {
       $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.');
 
       $this->assertNoText($new_name, $new_name . ' not found on node.');
-      $this->drupalGet($base_url . '/' . file_directory_path() . '/' . $upload->description, array('external' => TRUE));
+      $uri = 'public://' . $upload->description;
+      $external_uri = file_stream_wrapper_get_instance_by_uri($uri)->getExternalUrl();
+      $this->drupalGet($external_uri, array('external' => TRUE));
       $this->assertResponse(array(404), 'Uploaded ' . $upload->description . ' is not accessible.');
     }
     else {
@@ -119,17 +121,17 @@ class UploadTestCase extends DrupalWebTestCase {
     $text_file = current($this->drupalGetTestFiles('text'));
     // Select a file that's less than the 1MB upload limit so we only test one
     // limit at a time.
-    $this->uploadFile($node, $text_file->filepath, FALSE);
+    $this->uploadFile($node, $text_file->uri, FALSE);
     // Test the error message in two steps in case there are additional errors
     // that change the error message's format.
-    $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $text_file->filename)), t('File %filepath was not allowed to be uploaded', array('%filepath' => $text_file->filepath)));
+    $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $text_file->filename)), t('File %filepath was not allowed to be uploaded', array('%filepath' => $text_file->uri)));
     $this->assertRaw(t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $settings['upload_extensions'])), t('File extension cited as reason for failure'));
 
     // Attempt to upload .html file when .html is only extension allowed.
     $html_files = array_values($this->drupalGetTestFiles('html'));
     // Use the HTML file with the .html extension, $html_files[0] has a .txt
     // extension.
-    $html_file = $html_files[1]->filepath;
+    $html_file = $html_files[1]->uri;
     $this->uploadFile($node, $html_file);
     $this->assertNoRaw(t('The specified file %name could not be uploaded.', array('%name' => basename($html_file))), t('File ' . $html_file . ' was allowed to be uploaded'));
   }
@@ -139,7 +141,7 @@ class UploadTestCase extends DrupalWebTestCase {
    */
   function testLimit() {
     $files = $this->drupalGetTestFiles('text', 1310720); // 1 MB.
-    $file = current($files)->filepath;
+    $file = current($files)->uri;
 
     $admin_user = $this->drupalCreateUser(array('administer site configuration'));
     $web_user = $this->drupalCreateUser(array('access content', 'edit own page content', 'upload files', 'view uploaded files'));
@@ -206,8 +208,8 @@ class UploadTestCase extends DrupalWebTestCase {
    */
   function checkUploadedFile($filename) {
     global $base_url;
-    $file = file_directory_path() . '/' . $filename;
-    $this->drupalGet(file_create_url($file), array('external' => TRUE));
+    $file = 'public://' . $filename;
+    $this->drupalGet(file_external_url($file), array('external' => TRUE));
     $this->assertResponse(array(200), 'Uploaded ' . $filename . ' is accessible.');
     $this->assertTrue(strpos($this->drupalGetHeader('Content-Type'), 'text/plain') === 0, t('MIME type is text/plain.'));
     $this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of ' . $filename . ' verified.');
diff --git modules/user/user.admin.inc modules/user/user.admin.inc
index 1c44687..501b1d1 100644
--- modules/user/user.admin.inc
+++ modules/user/user.admin.inc
@@ -324,8 +324,12 @@ function user_admin_settings() {
   );
   // If picture support is enabled, check whether the picture directory exists.
   if (variable_get('user_pictures', 0)) {
-    $picture_path = file_create_path(variable_get('user_picture_path', 'pictures'));
-    file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path');
+    // TODO: Look up the default scheme to use, don't assume public.
+    $picture_path = 'public://' . variable_get('user_picture_path', 'pictures');
+    if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
+      form_set_error('user_picture_path', t('The directory %directory does not exist or is not writable.', array('%directory' => $picture_path)));
+      watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $picture_path), WATCHDOG_ERROR);
+    }
   }
   $picture_support = variable_get('user_pictures', 0);
   $form['personalization']['user_pictures'] = array(
diff --git modules/user/user.install modules/user/user.install
index 7a63b2f..77ef792 100644
--- modules/user/user.install
+++ modules/user/user.install
@@ -184,7 +184,7 @@ function user_schema() {
         'type' => 'int',
         'not null' => TRUE,
         'default' => 0,
-        'description' => "Foreign key: {files}.fid of user's picture.",
+        'description' => "Foreign key: {file}.fid of user's picture.",
       ),
       'init' => array(
         'type' => 'varchar',
@@ -405,7 +405,7 @@ function user_update_7003() {
 }
 
 /**
- * Add the user's pictures to the {files} table and make them managed files.
+ * Add the user's pictures to the {file} table and make them managed files.
  */
 function user_update_7004(&$sandbox) {
   $ret = array();
@@ -414,7 +414,7 @@ function user_update_7004(&$sandbox) {
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0,
-    'description' => t("Foriegn key: {files}.fid of user's picture."),
+    'description' => t("Foriegn key: {file}.fid of user's picture."),
   );
 
   if (!isset($sandbox['progress'])) {
@@ -428,13 +428,13 @@ function user_update_7004(&$sandbox) {
     // Initialize batch update information.
     $sandbox['progress'] = 0;
     $sandbox['last_user_processed'] = -1;
-    $sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} WHERE picture <> ''")->fetchField();
+    $sandbox['max'] = db_query("SELECT COUNT(*) FROM {user} WHERE picture <> ''")->fetchField();
   }
 
-  // As a batch operation move the photos into the {files} table and update the
+  // As a batch operation move the photos into the {file} table and update the
   // {users} records.
   $limit = 500;
-  $result = db_query_range("SELECT uid, picture FROM {users} WHERE picture <> '' AND uid > :uid ORDER BY uid", array(':uid' => $sandbox['last_user_processed']), 0, $limit);
+  $result = db_query_range("SELECT uid, picture FROM {user} WHERE picture <> '' AND uid > :uid ORDER BY uid", array(':uid' => $sandbox['last_user_processed']), 0, $limit);
   foreach ($result as $user) {
     // Don't bother adding files that don't exist.
     if (!file_exists($user->picture)) {
@@ -442,16 +442,16 @@ function user_update_7004(&$sandbox) {
     }
 
     // Check if the file already exists.
-    $files = file_load_multiple(array(), array('filepath' => $user->picture));
+    $files = file_load_multiple(array(), array('uri' => $user->picture));
     if (count($files)) {
       $file = reset($files);
     }
     else {
       // Create a file object.
       $file = new stdClass();
-      $file->filepath = $user->picture;
-      $file->filename = basename($file->filepath);
-      $file->filemime = file_get_mimetype($file->filepath);
+      $file->uri      = $user->picture;
+      $file->filename = basename($file->uri);
+      $file->filemime = file_get_mimetype($file->uri);
       $file->uid      = $user->uid;
       $file->status   = FILE_STATUS_PERMANENT;
       $file = file_save($file);
@@ -474,8 +474,8 @@ function user_update_7004(&$sandbox) {
   // When we're finished, drop the old picture field and rename the new one to
   // replace it.
   if (isset($ret['#finished']) && $ret['#finished'] == 1) {
-    db_drop_field($ret, 'users', 'picture');
-    db_change_field($ret, 'users', 'picture_fid', 'picture', $picture_field);
+    db_drop_field($ret, 'user', 'picture');
+    db_change_field($ret, 'user', 'picture_fid', 'picture', $picture_field);
   }
 
   return $ret;
diff --git modules/user/user.module modules/user/user.module
index 11d9b0b..646475c 100644
--- modules/user/user.module
+++ modules/user/user.module
@@ -404,8 +404,14 @@ function user_save($account, $edit = array(), $category = 'account') {
       // If the picture is a temporary file move it to its final location and
       // make it permanent.
       if (($picture->status & FILE_STATUS_PERMANENT) == 0) {
-        $info = image_get_info($picture->filepath);
-        $destination = file_create_path(variable_get('user_picture_path', 'pictures') . '/picture-' . $account->uid . '.' . $info['extension']);
+        $info = image_get_info($picture->uri);
+        // TODO: Lookup a private/public setting. Or user_picture_path should include private:// or public://.
+        $picture_directory = 'public://' . variable_get('user_picture_path', 'pictures');
+        
+        // Prepare the pictures directory.
+        file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
+        $destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '.' . $info['extension']);
+
         if ($picture = file_move($picture, $destination, FILE_EXISTS_REPLACE)) {
           $picture->status |= FILE_STATUS_PERMANENT;
           $edit['picture'] = file_save($picture);
@@ -796,8 +802,8 @@ function user_permission() {
  * Ensure that user pictures (avatars) are always downloadable.
  */
 function user_file_download($filepath) {
-  if (strpos($filepath, variable_get('user_picture_path', 'pictures') . '/picture-') === 0) {
-    $info = image_get_info(file_create_path($filepath));
+  if (strpos(file_uri_target($filepath), variable_get('user_picture_path', 'pictures') . '/picture-') === 0) {
+    $info = image_get_info($filepath);
     return array('Content-Type' => $info['mime_type']);
   }
 }
@@ -1167,8 +1173,8 @@ function template_preprocess_user_picture(&$variables) {
       if (is_numeric($account->picture)) {
         $account->picture = file_load($account->picture);
       }
-      if (!empty($account->picture->filepath)) {
-        $filepath = $account->picture->filepath;
+      if (!empty($account->picture->uri)) {
+        $filepath = $account->picture->uri;
       }
     }
     elseif (variable_get('user_picture_default', '')) {
diff --git modules/user/user.test modules/user/user.test
index fd63b2a..f2d38c9 100644
--- modules/user/user.test
+++ modules/user/user.test
@@ -634,13 +634,17 @@ class UserPictureTestCase extends DrupalWebTestCase {
     $this->user = $this->drupalCreateUser();
 
     // Test if directories specified in settings exist in filesystem.
-    $file_dir = file_directory_path();
-    $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path');
+    $file_dir = 'public://';
+    $file_check = file_prepare_directory($file_dir, FILE_CREATE_DIRECTORY);
+
+    /*
+      TODO: Add capability to store photos publicly or privately, but tests will be the same.
+    */
 
     $picture_dir = variable_get('user_picture_path', 'pictures');
-    $picture_path = $file_dir . '/' . $picture_dir;
+    $picture_path = $file_dir . $picture_dir;
 
-    $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path');
+    $pic_check = file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY);
     $this->_directory_test = is_writable($picture_path);
     $this->assertTrue($this->_directory_test, "The directory $picture_path doesn't exist or is not writable. Further tests won't be made.");
   }
@@ -667,7 +671,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         $this->drupalLogin($this->user);
 
         $image = current($this->drupalGetTestFiles('image'));
-        $info = image_get_info($image->filepath);
+        $info = image_get_info($image->uri);
 
         // Set new variables: invalid dimensions, valid filesize (0 = no limit).
         $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10);
@@ -704,7 +708,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         // Images are sorted first by size then by name. We need an image
         // bigger than 1 KB so we'll grab the last one.
         $image = end($this->drupalGetTestFiles('image'));
-        $info = image_get_info($image->filepath);
+        $info = image_get_info($image->uri);
 
         // Set new variables: valid dimensions, invalid filesize.
         $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
@@ -717,7 +721,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         // Test that the upload failed and that the correct reason was cited.
         $text = t('The specified file %filename could not be uploaded.', array('%filename' => $image->filename));
         $this->assertRaw($text, t('Upload failed.'));
-        $text = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size(filesize($image->filepath)), '%maxsize' => format_size($test_size * 1024)));
+        $text = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size(filesize($image->uri)), '%maxsize' => format_size($test_size * 1024)));
         $this->assertRaw($text, t('File size cited as reason for failure.'));
 
         // Check if file is not uploaded.
@@ -739,7 +743,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         $this->drupalLogin($this->user);
 
         $image = current($this->drupalGetTestFiles('image'));
-        $info = image_get_info($image->filepath);
+        $info = image_get_info($image->uri);
 
         // Set new variables: invalid dimensions, valid filesize (0 = no limit).
         $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10);
@@ -772,7 +776,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         $this->drupalLogin($this->user);
 
         $image = current($this->drupalGetTestFiles('image'));
-        $info = image_get_info($image->filepath);
+        $info = image_get_info($image->uri);
 
         // Set new variables: valid dimensions, invalid filesize.
         $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
@@ -785,7 +789,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
         // Test that the upload failed and that the correct reason was cited.
         $text = t('The specified file %filename could not be uploaded.', array('%filename' => $image->filename));
         $this->assertRaw($text, t('Upload failed.'));
-        $text = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size(filesize($image->filepath)), '%maxsize' => format_size($test_size * 1024)));
+        $text = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size(filesize($image->uri)), '%maxsize' => format_size($test_size * 1024)));
         $this->assertRaw($text, t('File size cited as reason for failure.'));
 
         // Check if file is not uploaded.
@@ -804,7 +808,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
       $this->drupalLogin($this->user);
 
       $image = current($this->drupalGetTestFiles('image'));
-      $info = image_get_info($image->filepath);
+      $info = image_get_info($image->uri);
 
       // Set new variables: valid dimensions, valid filesize (0 = no limit).
       $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
@@ -815,7 +819,7 @@ class UserPictureTestCase extends DrupalWebTestCase {
 
       // Check if image is displayed in user's profile page.
       $this->drupalGet('user');
-      $this->assertRaw($pic_path, t("Image is displayed in user's profile page"));
+      $this->assertRaw(file_uri_target($pic_path), t("Image is displayed in user's profile page"));
 
       // Check if file is located in proper directory.
       $this->assertTrue(is_file($pic_path), t('File is located in proper directory'));
@@ -823,12 +827,12 @@ class UserPictureTestCase extends DrupalWebTestCase {
   }
 
   function saveUserPicture($image) {
-    $edit = array('files[picture_upload]' => realpath($image->filepath));
+    $edit = array('files[picture_upload]' => drupal_realpath($image->uri));
     $this->drupalPost('user/' . $this->user->uid . '/edit', $edit, t('Save'));
 
-    $img_info = image_get_info($image->filepath);
+    $img_info    = image_get_info($image->uri);
     $picture_dir = variable_get('user_picture_path', 'pictures');
-    $pic_path = file_directory_path() . '/' . $picture_dir . '/picture-' . $this->user->uid . '.' . $img_info['extension'];
+    $pic_path    = 'public://' . $picture_dir . '/picture-' . $this->user->uid . '.' . $img_info['extension'];
 
     return $pic_path;
   }
diff --git scripts/code-clean.sh scripts/code-clean.sh
old mode 100644
new mode 100755
diff --git scripts/cron-curl.sh scripts/cron-curl.sh
old mode 100644
new mode 100755
diff --git scripts/cron-lynx.sh scripts/cron-lynx.sh
old mode 100644
new mode 100755
diff --git scripts/run-tests.sh scripts/run-tests.sh
index af48ac8..9fa8d09 100755
--- scripts/run-tests.sh
+++ scripts/run-tests.sh
@@ -400,7 +400,7 @@ function simpletest_script_get_test_list() {
     elseif ($args['file']) {
       $files = array();
       foreach ($args['test_names'] as $file) {
-        $files[realpath($file)] = 1;
+        $files[drupal_realpath($file)] = 1;
       }
 
       // Check for valid class names.
diff --git themes/garland/template.php themes/garland/template.php
index eed4977..3884db2 100644
--- themes/garland/template.php
+++ themes/garland/template.php
@@ -79,9 +79,9 @@ function garland_node_submitted($node) {
 function garland_get_ie_styles() {
   global $language;
 
-  $ie_styles = '<link type="text/css" rel="stylesheet" media="all" href="' . base_path() . path_to_theme() . '/fix-ie.css" />' . "\n";
+  $ie_styles = '<link type="text/css" rel="stylesheet" media="all" href="' . file_external_url(path_to_theme() . '/fix-ie.css') . '" />' . "\n";
   if ($language->direction == LANGUAGE_RTL) {
-    $ie_styles .= '      <style type="text/css" media="all">@import "' . base_path() . path_to_theme() . '/fix-ie-rtl.css";</style>' . "\n";
+    $ie_styles .= '      <style type="text/css" media="all">@import "' . file_external_url(path_to_theme() . '/fix-ie-rtl.css') . '";</style>' . "\n";
   }
 
   return $ie_styles;
