diff --git a/lib/filelib.php b/lib/filelib.php
index 523c64333d7..f1e44f3262e 100644
--- a/lib/filelib.php
+++ b/lib/filelib.php
@@ -3006,14 +3006,36 @@ class curl {
* Calls {@link multi()} with specific download headers
*
*
- * $c = new curl;
+ * $c = new curl();
+ * $file1 = fopen('a', 'wb');
+ * $file2 = fopen('b', 'wb');
* $c->download(array(
- * array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')),
- * array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb'))
+ * array('url'=>'http://localhost/', 'file'=>$file1),
+ * array('url'=>'http://localhost/20/', 'file'=>$file2)
+ * ));
+ * fclose($file1);
+ * fclose($file2);
+ *
+ *
+ * or
+ *
+ *
+ * $c = new curl();
+ * $c->download(array(
+ * array('url'=>'http://localhost/', 'filepath'=>'/tmp/file1.tmp'),
+ * array('url'=>'http://localhost/20/', 'filepath'=>'/tmp/file2.tmp')
* ));
*
*
- * @param array $requests An array of files to request
+ * @param array $requests An array of files to request {
+ * url => url to download the file [required]
+ * file => file handler, or
+ * filepath => file path
+ * }
+ * If 'file' and 'filepath' parameters are both specified in one request, the
+ * open file handle in the 'file' parameter will take precedence and 'filepath'
+ * will be ignored.
+ *
* @param array $options An array of options to set
* @return array An array of results
*/
@@ -3037,11 +3059,15 @@ class curl {
$results = array();
$main = curl_multi_init();
for ($i = 0; $i < $count; $i++) {
- $url = $requests[$i];
- foreach($url as $n=>$v){
- $options[$n] = $url[$n];
+ if (!empty($requests[$i]['filepath']) and empty($requests[$i]['file'])) {
+ // open file
+ $requests[$i]['file'] = fopen($requests[$i]['filepath'], 'w');
+ $requests[$i]['auto-handle'] = true;
}
- $handles[$i] = curl_init($url['url']);
+ foreach($requests[$i] as $n=>$v){
+ $options[$n] = $v;
+ }
+ $handles[$i] = curl_init($requests[$i]['url']);
$this->apply_opt($handles[$i], $options);
curl_multi_add_handle($main, $handles[$i]);
}
@@ -3058,6 +3084,13 @@ class curl {
curl_multi_remove_handle($main, $handles[$i]);
}
curl_multi_close($main);
+
+ for ($i = 0; $i < $count; $i++) {
+ if (!empty($requests[$i]['filepath']) and !empty($requests[$i]['auto-handle'])) {
+ // close file handler if file is opened in this function
+ fclose($requests[$i]['file']);
+ }
+ }
return $results;
}
diff --git a/repository/alfresco/lib.php b/repository/alfresco/lib.php
index 705e6e96414..b5a871f3124 100644
--- a/repository/alfresco/lib.php
+++ b/repository/alfresco/lib.php
@@ -206,6 +206,7 @@ class repository_alfresco extends repository {
$fp = fopen($path, 'w');
$c = new curl;
$c->download(array(array('url'=>$url, 'file'=>$fp)));
+ fclose($fp);
return array('path'=>$path, 'url'=>$url);
}
diff --git a/repository/flickr/lib.php b/repository/flickr/lib.php
index 688663899a3..7d3dcd67528 100644
--- a/repository/flickr/lib.php
+++ b/repository/flickr/lib.php
@@ -258,6 +258,7 @@ class repository_flickr extends repository {
$c->download(array(
array('url'=>$url, 'file'=>$fp)
));
+ fclose($fp);
return array('path'=>$path, 'url'=>$url);
}