Skip to content

Instantly share code, notes, and snippets.

@pborreli
Last active August 23, 2020 09:48
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pborreli/5546547 to your computer and use it in GitHub Desktop.
Save pborreli/5546547 to your computer and use it in GitHub Desktop.
Show progress of a file download inside Symfony 2.3 console #howto
<?php
protected function execute(InputInterface $input, OutputInterface $output)
{
$progress = $this->getHelperSet()->get('progress');
$ctx = stream_context_create(array(), array('notification' => function ($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) use ($output, $progress) {
switch ($notification_code) {
case STREAM_NOTIFY_FILE_SIZE_IS:
$progress->start($output, $bytes_max);
break;
case STREAM_NOTIFY_PROGRESS:
$progress->setCurrent($bytes_transferred);
break;
}
}));
$file = file_get_contents($url, false, $ctx);
$progress->finish();
}
Copy link

ghost commented May 9, 2013

nice! Add to the sf docs...

@mykiwi
Copy link

mykiwi commented May 9, 2013

👍

@fabpot
Copy link

fabpot commented May 9, 2013

Indeed, that would be a good addition to the docs.

@fonsecas72
Copy link

👍 thank you for this.

@fonsecas72
Copy link

I had to change $progress->start($output, $bytes_max); to $progress->start($bytes_max); on last symfony/console version though (2.6.4) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment