Skip to content

Commit 874bc8c

Browse files
committed
Add additional progress bar example
1 parent 55214cd commit 874bc8c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

examples/progress.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use \Curl\Curl;
55

66
$curl = new Curl();
7-
$curl->progress(function ($client, $download_size, $downloaded, $upload_size, $uploaded){
7+
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
88
if ($download_size === 0) {
99
return;
1010
}
1111

12-
$progress = floor( $downloaded * 100 / $download_size );
13-
echo ' ' . $progress . '%' . "\r";
12+
$percent = floor( $downloaded * 100 / $download_size );
13+
echo ' ' . $percent . '%' . "\r";
1414
});
1515
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');

examples/progress_advanced.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
require '../src/Curl/Curl.php';
3+
4+
use \Curl\Curl;
5+
6+
$curl = new Curl();
7+
$curl->progress(function($client, $download_size, $downloaded, $upload_size, $uploaded) {
8+
if ($download_size === 0) {
9+
return;
10+
}
11+
12+
// Display a progress bar: xxx% [=======> ]
13+
$percent = (int)floor( $downloaded * 100 / $download_size );
14+
$percentage = sprintf('%3d%%', $percent);
15+
$arrow_length = 40;
16+
$arrow_tail_length = max(1, floor(($percent / 100 * $arrow_length) - 3));
17+
$space_length = max(0, $arrow_length - $arrow_tail_length - 3);
18+
$arrow = '[' . str_repeat('=', $arrow_tail_length) . '>' . str_repeat(' ', $space_length) . ']';
19+
echo ' ' . $percentage . ' ' . $arrow . "\r";
20+
});
21+
$curl->complete(function($instance) {
22+
echo "\n" . 'download complete' . "\n";
23+
});
24+
$curl->download('https://php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');

0 commit comments

Comments
 (0)