Skip to content

Commit fa3cfd3

Browse files
authored
Merge pull request php-curl-class#615 from zachborboa/pr608
Implement Curl::setRange() and MultiCurl::setRange()
2 parents 9fde3ff + cf9ab59 commit fa3cfd3

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

src/Curl/Curl.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function download($url, $mixed_filename)
309309
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
310310
$first_byte_position = $filesize;
311311
$range = $first_byte_position . '-';
312-
$this->setOpt(CURLOPT_RANGE, $range);
312+
$this->setRange($range);
313313
$this->fileHandle = fopen($download_filename, 'ab');
314314
} else {
315315
$this->fileHandle = fopen($download_filename, 'wb');
@@ -326,7 +326,7 @@ public function download($url, $mixed_filename)
326326
};
327327
}
328328

329-
$this->setOpt(CURLOPT_FILE, $this->fileHandle);
329+
$this->setFile($this->fileHandle);
330330
$this->get($url);
331331

332332
return ! $this->error;
@@ -1123,6 +1123,17 @@ public function unsetProxy()
11231123
$this->setOpt(CURLOPT_PROXY, null);
11241124
}
11251125

1126+
/**
1127+
* Set Range
1128+
*
1129+
* @access public
1130+
* @param $range
1131+
*/
1132+
public function setRange($range)
1133+
{
1134+
$this->setOpt(CURLOPT_RANGE, $range);
1135+
}
1136+
11261137
/**
11271138
* Set Referer
11281139
*
@@ -1597,7 +1608,7 @@ private function downloadComplete($fh)
15971608

15981609
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
15991610
// resource has gone away, resetting to default".
1600-
$this->setOpt(CURLOPT_FILE, STDOUT);
1611+
$this->setFile(STDOUT);
16011612

16021613
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
16031614
// responses as the return value of curl_exec(). Without this,

src/Curl/MultiCurl.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function addDownload($url, $mixed_filename)
102102
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
103103
$first_byte_position = $filesize;
104104
$range = $first_byte_position . '-';
105-
$curl->setOpt(CURLOPT_RANGE, $range);
105+
$curl->setRange($range);
106106
$curl->fileHandle = fopen($download_filename, 'ab');
107107

108108
// Move the downloaded temporary file to the destination save path.
@@ -122,7 +122,7 @@ public function addDownload($url, $mixed_filename)
122122
}
123123
}
124124

125-
$curl->setOpt(CURLOPT_FILE, $curl->fileHandle);
125+
$curl->setFile($curl->fileHandle);
126126
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
127127
$curl->setOpt(CURLOPT_HTTPGET, true);
128128
return $curl;
@@ -702,6 +702,17 @@ public function setOpts($options)
702702
}
703703
}
704704

705+
/**
706+
* Set Range
707+
*
708+
* @access public
709+
* @param $range
710+
*/
711+
public function setRange($range)
712+
{
713+
$this->setOpt(CURLOPT_RANGE, $range);
714+
}
715+
705716
/**
706717
* Set Referer
707718
*

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3886,7 +3886,16 @@ public function testSetFile()
38863886
$file = STDOUT;
38873887

38883888
$curl = new Curl();
3889-
$curl->setFile(STDOUT);
3889+
$curl->setFile($file);
38903890
$this->assertEquals($file, $curl->getOpt(CURLOPT_FILE));
38913891
}
3892+
3893+
public function testSetRange()
3894+
{
3895+
$range = '1000-';
3896+
3897+
$curl = new Curl();
3898+
$curl->setRange($range);
3899+
$this->assertEquals($range, $curl->getOpt(CURLOPT_RANGE));
3900+
}
38923901
}

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3397,7 +3397,17 @@ public function testSetFile()
33973397

33983398
$multi_curl = new MultiCurl();
33993399
$this->assertNull($multi_curl->getOpt(CURLOPT_FILE));
3400-
$multi_curl->setFile(STDOUT);
3400+
$multi_curl->setFile($file);
34013401
$this->assertEquals($file, $multi_curl->getOpt(CURLOPT_FILE));
34023402
}
3403+
3404+
public function testSetRange()
3405+
{
3406+
$range = '1000-';
3407+
3408+
$multi_curl = new MultiCurl();
3409+
$this->assertNull($multi_curl->getOpt(CURLOPT_RANGE));
3410+
$multi_curl->setRange($range);
3411+
$this->assertEquals($range, $multi_curl->getOpt(CURLOPT_RANGE));
3412+
}
34033413
}

0 commit comments

Comments
 (0)