diff --git a/README.md b/README.md index 6643f90b..91c1577c 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,19 @@ Delete an empty bucket: S3::deleteBucket($bucketName) ``` +### Progress Function + +Add a progress function when S3 downloading / uploading + +```php +S3::setProgressFunction('progress'); + +function progress($resource,$download_size, $downloaded, $upload_size, $uploaded) +{ + if($download_size > 0) + echo $downloaded / $download_size * 100; + ob_flush(); + flush(); + sleep(1); // just to see effect +} +``` diff --git a/S3.php b/S3.php index 83c01733..27c57174 100644 --- a/S3.php +++ b/S3.php @@ -190,6 +190,15 @@ class S3 */ private static $__signingKeyResource = false; + /** + * CURL progress function callback + * + * @var function + * @access public + * @static + */ + public static $progressFunction = null; + /** * Constructor - if you're not using the class statically @@ -355,6 +364,17 @@ public static function freeSigningKey() openssl_free_key(self::$__signingKeyResource); } + /** + * Set progress function + * + * @param function $func Progress function + * @return void + */ + public static function setProgressFunction($func = null) + { + self::$progressFunction = $func; + } + /** * Internal error handler @@ -2237,6 +2257,12 @@ public function getResponse() default: break; } + // set curl progress function callback + if (S3::$progressFunction) { + curl_setopt($curl, CURLOPT_NOPROGRESS, false); + curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, S3::$progressFunction); + } + // Execute, grab errors if (curl_exec($curl)) $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);