plonknimbuzz
plonknimbuzz

Reputation: 2664

how to make progress with php in cmd like composer/curl/wget did

i have php script like this:

echo 'Loading...'.PHP_EOL;
if($download_size > 0)
        {
            echo round($downloaded / $download_size  * 100, 2) .'%'. PHP_EOL;
        }

this return

Loading ...
0%
0%
0%
0%
0%
0%
0%
0%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
0.01%
--snippet--

when we use compoeser, the download progress will be like this

githubUser/githubChannel 75%

wget/curl/apt-get will be like this

file.ext [=======> ] 40%

how to do this with php run in cli mode?

i try add system('cls'); after echo but not work

what i want is like this

progress: 0%

after few second

progress: 2%

after few second

progress: 6%

after few second

progress: 9%

thanks .

Upvotes: 0

Views: 168

Answers (1)

user4121362
user4121362

Reputation:

You have to use the \r character (carriage return). When "written" to your terminal, the cursor pointing to the column of the next character to be written will be moved to the first column of the actual line.

A complete working example would look like the following snippet:

<?php

$download_size = 100;
$downloaded = 0;

echo 'Loading...'.PHP_EOL;
while ($downloaded < $download_size) {
    if($download_size > 0) {
        echo round(($downloaded / $download_size) * 100, 2) . "%\r";
        sleep(1);
        $downloaded += 1;
    }
}

Upvotes: 1

Related Questions