Skip to content

Commit 91b45f2

Browse files
authored
Merge pull request php-curl-class#375 from zachborboa/master
Fix "Too many open files" error
2 parents 484d87f + a291832 commit 91b45f2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/Curl/Curl.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Curl
6969
public $successFunction = null;
7070
public $errorFunction = null;
7171
public $completeFunction = null;
72+
public $fileHandle = null;
7273

7374
private $cookies = array();
7475
private $responseCookies = array();
@@ -336,12 +337,12 @@ public function error($callback)
336337
public function exec($ch = null)
337338
{
338339
$this->responseCookies = array();
339-
if (!($ch === null)) {
340-
$this->rawResponse = curl_multi_getcontent($ch);
341-
} else {
340+
if ($ch === null) {
342341
$this->call($this->beforeSendFunction);
343342
$this->rawResponse = curl_exec($this->curl);
344343
$this->curlErrorCode = curl_errno($this->curl);
344+
} else {
345+
$this->rawResponse = curl_multi_getcontent($ch);
345346
}
346347
$this->curlErrorMessage = curl_error($this->curl);
347348
$this->curlError = !($this->curlErrorCode === 0);

src/Curl/MultiCurl.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class MultiCurl
77
public $baseUrl = null;
88
public $multiCurl;
99
public $curls = array();
10-
private $curlFileHandles = array();
1110
private $nextCurlId = 1;
1211
private $isStarted = false;
1312

@@ -75,20 +74,23 @@ public function addDownload($url, $mixed_filename)
7574
$curl = new Curl();
7675
$curl->setURL($url);
7776

77+
// Use tmpfile() or php://temp to avoid "Too many open files" error.
7878
if (is_callable($mixed_filename)) {
7979
$callback = $mixed_filename;
8080
$curl->downloadCompleteFunction = $callback;
81-
$fh = tmpfile();
81+
$curl->fileHandle = tmpfile();
8282
} else {
8383
$filename = $mixed_filename;
84-
$fh = fopen($filename, 'wb');
84+
$curl->downloadCompleteFunction = function($instance, $fh) use ($filename) {
85+
file_put_contents($filename, stream_get_contents($fh));
86+
};
87+
$curl->fileHandle = fopen('php://temp', 'wb');
8588
}
8689

87-
$curl->setOpt(CURLOPT_FILE, $fh);
90+
$curl->setOpt(CURLOPT_FILE, $curl->fileHandle);
8891
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
8992
$curl->setOpt(CURLOPT_HTTPGET, true);
9093
$this->addHandle($curl);
91-
$this->curlFileHandles[$curl->id] = $fh;
9294
return $curl;
9395
}
9496

@@ -516,6 +518,10 @@ public function setUserAgent($user_agent)
516518
*/
517519
public function start()
518520
{
521+
if ($this->isStarted) {
522+
return;
523+
}
524+
519525
foreach ($this->curls as $ch) {
520526
$this->initHandle($ch);
521527
}
@@ -536,9 +542,8 @@ public function start()
536542
unset($this->curls[$key]);
537543

538544
// Close open file handles and reset the curl instance.
539-
if (isset($this->curlFileHandles[$ch->id])) {
540-
$ch->downloadComplete($this->curlFileHandles[$ch->id]);
541-
unset($this->curlFileHandles[$ch->id]);
545+
if (!($ch->fileHandle === null)) {
546+
$ch->downloadComplete($ch->fileHandle);
542547
}
543548
break;
544549
}

0 commit comments

Comments
 (0)