Add additional progress bar example

This commit is contained in:
Zach Borboa
2015-03-03 03:12:04 +07:00
parent 55214cddb5
commit 874bc8c7b0
2 changed files with 27 additions and 3 deletions
+3 -3
View File
@@ -4,12 +4,12 @@ require '../src/Curl/Curl.php';
use \Curl\Curl;
$curl = new Curl();
$curl->progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded){
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
if ($download_size === 0) {
return;
}
$progress = floor( $downloaded * 100 / $download_size );
echo ' ' . $progress . '%' . "\r";
$percent = floor( $downloaded * 100 / $download_size );
echo ' ' . $percent . '%' . "\r";
});
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');
+24
View File
@@ -0,0 +1,24 @@
<?php
require '../src/Curl/Curl.php';
use \Curl\Curl;
$curl = new Curl();
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
if ($download_size === 0) {
return;
}
// Display a progress bar: xxx% [=======> ]
$percent = (int)floor( $downloaded * 100 / $download_size );
$percentage = sprintf('%3d%%', $percent);
$arrow_length = 40;
$arrow_tail_length = max(1, floor(($percent / 100 * $arrow_length) - 3));
$space_length = max(0, $arrow_length - $arrow_tail_length - 3);
$arrow = '[' . str_repeat('=', $arrow_tail_length) . '>' . str_repeat(' ', $space_length) . ']';
echo ' ' . $percentage . ' ' . $arrow . "\r";
});
$curl->complete(function($instance) {
echo "\n" . 'download complete' . "\n";
});
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');