Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-27125 better manage file handles when downloading multiple files.

Based on Dongsheng Cai's branch: s11_MDL-27125_curl_file_handler_master
  • Loading branch information...
commit f0da446c567c3e2b16981e58269658822a4c2127 1 parent 17a759c
@doctorlard doctorlard authored samhemelryk committed
View
49 lib/filelib.php
@@ -2737,14 +2737,36 @@ private function apply_opt($curl, $options) {
* Calls {@link multi()} with specific download headers
*
* <code>
- * $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);
+ * </code>
+ *
+ * or
+ *
+ * <code>
+ * $c = new curl();
+ * $c->download(array(
+ * array('url'=>'http://localhost/', 'filepath'=>'/tmp/file1.tmp'),
+ * array('url'=>'http://localhost/20/', 'filepath'=>'/tmp/file2.tmp')
* ));
* </code>
*
- * @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
*/
@@ -2767,11 +2789,15 @@ protected function multi($requests, $options = array()) {
$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;
+ }
+ foreach($requests[$i] as $n=>$v){
+ $options[$n] = $v;
}
- $handles[$i] = curl_init($url['url']);
+ $handles[$i] = curl_init($requests[$i]['url']);
$this->apply_opt($handles[$i], $options);
curl_multi_add_handle($main, $handles[$i]);
}
@@ -2788,6 +2814,13 @@ protected function multi($requests, $options = array()) {
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;
}
/**
View
1  repository/alfresco/lib.php
@@ -200,6 +200,7 @@ public function get_file($uuid, $file = '') {
$fp = fopen($path, 'w');
$c = new curl;
$c->download(array(array('url'=>$url, 'file'=>$fp)));
+ fclose($fp);
return array('path'=>$path, 'url'=>$url);
}
View
1  repository/flickr/lib.php
@@ -258,6 +258,7 @@ public function get_file($photo_id, $file = '') {
$c->download(array(
array('url'=>$url, 'file'=>$fp)
));
+ fclose($fp);
return array('path'=>$path, 'url'=>$url);
}
Please sign in to comment.
Something went wrong with that request. Please try again.