diff --git a/inc/io.php b/inc/io.php
index c25bd89754487f72c81c750b000cb4dfa645fd9b..1939dc6a02ca46a0dad7f598f4502912398f134b 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -259,22 +259,52 @@ function io_mkdir_ftp($dir){
 }
 
 /**
- * downloads a file from the net and saves it to the given location
+ * downloads a file from the net and saves it
+ *
+ * if $useAttachment is false, 
+ * - $file is the full filename to save the file, incl. path
+ * - if successful will return true, false otherwise
+ 
+ * if $useAttachment is true, 
+ * - $file is the directory where the file should be saved
+ * - if successful will return the name used for the saved file, false otherwise
  *
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Chris Smith <chris@jalakai.co.uk>
  */
-function io_download($url,$file){
+function io_download($url,$file,$useAttachment=false,$defaultName=''){
   $http = new DokuHTTPClient();
   $http->max_bodysize = 2*1024*1024; //max. 2MB
   $http->timeout = 25; //max. 25 sec
 
   $data = $http->get($url);
   if(!$data) return false;
+  
+  if ($useAttachment) {
+    $name = '';
+      if (isset($http->resp_headers['content-disposition'])) {
+      $content_disposition = $http->resp_headers['content-disposition'];
+      if (is_string($content_disposition) && 
+          preg_match('/attachment;\s*filename\s*=\s*"([^"]*)"/i', $content_disposition, $match=array())) {
+          
+          $name = basename($match[1]);
+      }
+          
+    }
+    
+    if (!$name) {
+        if (!$defaultName) return false;
+        $name = $defaultName;        
+    }
+    
+    $file = $file.$name;
+  }
 
   $fp = @fopen($file,"w");
   if(!$fp) return false;
   fwrite($fp,$data);
   fclose($fp);
+  if ($useAttachment) return $name;
   return true;
 }
 
diff --git a/lib/plugins/plugin/admin.php b/lib/plugins/plugin/admin.php
index 091d416983b25eff996dc3fa50208d2c0e4e0d45..e22a8a4b8822abb8e4b5dae6e2d6d4db458afd6e 100644
--- a/lib/plugins/plugin/admin.php
+++ b/lib/plugins/plugin/admin.php
@@ -275,7 +275,7 @@ class ap_manage {
             return false;
           }
           
-          if (!io_download($url, "$tmp/$file")) {
+          if (!$file = io_download($url, "$tmp/", true, $file)) {
             $this->manager->error = sprintf($this->lang['error_download'],$url)."\n";
           }