Skip to content

Commit a01f3b6

Browse files
committed
Added CURL progress function callback when downloading/uploading.
1 parent 9cf2eec commit a01f3b6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ Delete an empty bucket:
9797
S3::deleteBucket($bucketName)
9898
```
9999

100+
### Progress Function
101+
102+
Add a progress function when S3 downloading / uploading
103+
104+
```php
105+
S3::setProgressFunction('progress');
106+
107+
function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
108+
{
109+
if($download_size > 0)
110+
echo $downloaded / $download_size * 100;
111+
ob_flush();
112+
flush();
113+
sleep(1); // just to see effect
114+
}
115+
```

S3.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ class S3
190190
*/
191191
private static $__signingKeyResource = false;
192192

193+
/**
194+
* CURL progress function callback
195+
*
196+
* @var function
197+
* @access public
198+
* @static
199+
*/
200+
public static $progressFunction = null;
201+
193202

194203
/**
195204
* Constructor - if you're not using the class statically
@@ -355,6 +364,17 @@ public static function freeSigningKey()
355364
openssl_free_key(self::$__signingKeyResource);
356365
}
357366

367+
/**
368+
* Set progress function
369+
*
370+
* @param function $func Progress function
371+
* @return void
372+
*/
373+
public static function setProgressFunction($func = null)
374+
{
375+
self::$progressFunction = $func;
376+
}
377+
358378

359379
/**
360380
* Internal error handler
@@ -2237,6 +2257,12 @@ public function getResponse()
22372257
default: break;
22382258
}
22392259

2260+
// set curl progress function callback
2261+
if (S3::$progressFunction) {
2262+
curl_setopt($curl, CURLOPT_NOPROGRESS, false);
2263+
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, S3::$progressFunction);
2264+
}
2265+
22402266
// Execute, grab errors
22412267
if (curl_exec($curl))
22422268
$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

0 commit comments

Comments
 (0)