Skip to content

Instantly share code, notes, and snippets.

@beyerz
Created August 3, 2017 07:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save beyerz/703d7a0993b8192cae4364873232417b to your computer and use it in GitHub Desktop.
Save beyerz/703d7a0993b8192cae4364873232417b to your computer and use it in GitHub Desktop.
Symfony console, download remote file with progress bar using guzzle
/**
* @param SymfonyStyle $io
* @param $remotePath
*
* @return string
*/
public function download(SymfonyStyle $io, $remotePath)
{
$io->section("Download " . $remotePath);
$parts = parse_url($remotePath);
$filename = basename($parts['path']);
$progress = null;
$tmp = sys_get_temp_dir();
$fileLocation = $tmp . DIRECTORY_SEPARATOR . $filename;
$handle = fopen($fileLocation, "w+");
$guzzle = new Client(array(
'progress' => function ($total, $downloaded) use ($io, &$progress) {
if ($total > 0 && is_null($progress)) {
$progress = $io->createProgressBar($total);
$progress->start();
}
if (!is_null($progress)) {
if ($total === $downloaded) {
$progress->finish();
return;
}
$progress->setProgress($downloaded);
}
},
'sink' => $handle,
));
$guzzle->get($remotePath);
return $fileLocation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment