Skip to content

Commit b67d5d5

Browse files
authored
Merge pull request php-curl-class#556 from zachborboa/master
Remove temporary file when download fails
2 parents d4e5e4a + ca36388 commit b67d5d5

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Curl::getCurl()
215215
Curl::getCurlErrorCode()
216216
Curl::getCurlErrorMessage()
217217
Curl::getDownloadCompleteCallback()
218+
Curl::getDownloadFileName()
218219
Curl::getErrorCallback()
219220
Curl::getErrorCode()
220221
Curl::getErrorMessage()

src/Curl/Curl.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Curl
3939
public $errorCallback = null;
4040
public $completeCallback = null;
4141
public $fileHandle = null;
42+
private $downloadFileName = null;
4243

4344
public $attempts = 0;
4445
public $retries = 0;
@@ -281,6 +282,7 @@ public function download($url, $mixed_filename)
281282
{
282283
if (is_callable($mixed_filename)) {
283284
$this->downloadCompleteCallback = $mixed_filename;
285+
$this->downloadFileName = null;
284286
$this->fileHandle = tmpfile();
285287
} else {
286288
$filename = $mixed_filename;
@@ -299,6 +301,7 @@ public function download($url, $mixed_filename)
299301
$range = $first_byte_position . '-';
300302
$this->setOpt(CURLOPT_RANGE, $range);
301303
}
304+
$this->downloadFileName = $download_filename;
302305
$this->fileHandle = fopen($download_filename, $mode);
303306

304307
// Move the downloaded temporary file to the destination save path.
@@ -1382,6 +1385,11 @@ public function getDownloadCompleteCallback()
13821385
return $this->downloadCompleteCallback;
13831386
}
13841387

1388+
public function getDownloadFileName()
1389+
{
1390+
return $this->downloadFileName;
1391+
}
1392+
13851393
public function getSuccessCallback()
13861394
{
13871395
return $this->successCallback;
@@ -1541,7 +1549,9 @@ private function buildUrl($url, $mixed_data = '')
15411549
*/
15421550
private function downloadComplete($fh)
15431551
{
1544-
if (!$this->error && $this->downloadCompleteCallback) {
1552+
if ($this->error && is_file($this->downloadFileName)) {
1553+
@unlink($this->downloadFileName);
1554+
} elseif (!$this->error && $this->downloadCompleteCallback) {
15451555
rewind($fh);
15461556
$this->call($this->downloadCompleteCallback, $fh);
15471557
$this->downloadCompleteCallback = null;

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,18 @@ public function testDownloadRange()
810810
$this->assertFalse(file_exists($filename));
811811
}
812812

813+
public function testDownloadErrorDeleteTemporaryFile()
814+
{
815+
$destination = \Helper\get_tmp_file_path();
816+
817+
$test = new Test();
818+
$test->curl->setHeader('X-DEBUG-TEST', '404');
819+
$test->curl->download(Test::TEST_URL, $destination);
820+
821+
$this->assertFalse(file_exists($test->curl->getDownloadFileName()));
822+
$this->assertFalse(file_exists($destination));
823+
}
824+
813825
public function testMaxFilesize()
814826
{
815827
$tests = array(

tests/PHPCurlClass/server.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@
346346
echo '202 Accepted';
347347
echo ' (remaining failures: ' . $_SESSION['failures_remaining'] . ')';
348348
exit;
349+
} elseif ($test === '404') {
350+
header('HTTP/1.1 404 Not Found');
351+
echo '404 Not Found';
352+
exit;
349353
}
350354

351355
header('Content-Type: text/plain');

0 commit comments

Comments
 (0)