Fix "Too many open files" error when adding hundreds of downloads using MultiCurl:addDownload()

This commit is contained in:
Zach Borboa
2016-08-15 23:41:46 -07:00
parent 58a6568cab
commit a2918325ae
2 changed files with 10 additions and 8 deletions
+1
View File
@@ -69,6 +69,7 @@ class Curl
public $successFunction = null;
public $errorFunction = null;
public $completeFunction = null;
public $fileHandle = null;
private $cookies = array();
private $responseCookies = array();
+9 -8
View File
@@ -7,7 +7,6 @@ class MultiCurl
public $baseUrl = null;
public $multiCurl;
public $curls = array();
private $curlFileHandles = array();
private $nextCurlId = 1;
private $isStarted = false;
@@ -75,20 +74,23 @@ class MultiCurl
$curl = new Curl();
$curl->setURL($url);
// Use tmpfile() or php://temp to avoid "Too many open files" error.
if (is_callable($mixed_filename)) {
$callback = $mixed_filename;
$curl->downloadCompleteFunction = $callback;
$fh = tmpfile();
$curl->fileHandle = tmpfile();
} else {
$filename = $mixed_filename;
$fh = fopen($filename, 'wb');
$curl->downloadCompleteFunction = function($instance, $fh) use ($filename) {
file_put_contents($filename, stream_get_contents($fh));
};
$curl->fileHandle = fopen('php://temp', 'wb');
}
$curl->setOpt(CURLOPT_FILE, $fh);
$curl->setOpt(CURLOPT_FILE, $curl->fileHandle);
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
$curl->setOpt(CURLOPT_HTTPGET, true);
$this->addHandle($curl);
$this->curlFileHandles[$curl->id] = $fh;
return $curl;
}
@@ -540,9 +542,8 @@ class MultiCurl
unset($this->curls[$key]);
// Close open file handles and reset the curl instance.
if (isset($this->curlFileHandles[$ch->id])) {
$ch->downloadComplete($this->curlFileHandles[$ch->id]);
unset($this->curlFileHandles[$ch->id]);
if (!($ch->fileHandle === null)) {
$ch->downloadComplete($ch->fileHandle);
}
break;
}