Index: services/file_service/file_service.inc
===================================================================
--- services/file_service/file_service.inc	(revision 356)
+++ services/file_service/file_service.inc	(working copy)
@@ -66,6 +66,21 @@
     $update = 'fid';
   }
 
+  // Get the directory name for the location of the file:
+  $dir = dirname($file->filepath);
+
+  // Build the destination folder tree if it doesn't already exists.
+  // file_check_path() is just a set of calls to this function,
+  // so no need to call it again.
+  if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
+    return services_error("Destination directory for file could not be created or is not writeable.");
+  }
+
+  // If filename has not been specified extract it from filepath.
+  if (empty($file->filename)) {
+    $file->filename = trim(basename($file->filepath), '.');
+  }
+
   // Build the list of non-munged extensions.
   // @todo: this should not be here. we need to figure out the right place.
   // @todo: also isn't that repeated variable get a waste? I mean, I guess it
@@ -76,39 +91,27 @@
     variable_get('upload_extensions_default', 'jpg jpeg gif png txt html doc xls pdf ppt pps odt ods odp'));
   }
 
-  // Get the directory name for the location of the file:
-  $dir = dirname($file->filepath);
-  // Build the destination folder tree if it doesn't already exists.
-  if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
-    return services_error("Could not create destination directory for file.");
-  }
-
   // Update file object as necessary
-  $file->filepath = file_destination(file_create_path($file->filepath), FILE_EXISTS_RENAME);
-  // If filename has not been specified extract it from filepath.
-  if ((!isset($file->filename)) || (empty($file->filename))) {
-    $file->filename = trim(basename($file->filepath), '.');
-  }
   $file->filename = file_munge_filename($file->filename, $extensions, TRUE);
   $file->filemime = file_get_mimetype($file->filename);
+  $destination = file_destination(file_create_path($file->filepath), FILE_EXISTS_RENAME);
 
   // 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->filename .= '.txt';
+    // As the file may be named example.php.txt, we need to munge again to
+    // convert to example.php_.txt, then create the correct destination.
+    $file->filename = file_munge_filename($file->filename, $extensions, TRUE);
+    $destination = file_destination(file_create_path($file->filepath), FILE_EXISTS_RENAME);
   }
 
-  // If the destination is not provided, or is not writable, error our
-  if (empty($file->filepath) || file_check_path($file->filepath) === FALSE) {
-    return services_error("Destintion directory does not exist or is not writeable.");
+  // Write the file
+  if ($filepath = file_save_data(base64_decode($file->file), $destination)) {
+    $file->filepath = $filepath;
   }
-
-  //The filepath that ends up in the node must contain the filename
-  $file->filepath .= '/'. $file->filename;
-
-  // Write the file
-  if (!file_save_data(base64_decode($file->file), $file->filepath)) {
+  else {
     return services_error("Could not write file to destination");
   }
 
