Skip to content

Instantly share code, notes, and snippets.

@ivankristianto
Created October 1, 2015 05:05
Show Gist options
  • Save ivankristianto/8e4c2ff46e9c6162796b to your computer and use it in GitHub Desktop.
Save ivankristianto/8e4c2ff46e9c6162796b to your computer and use it in GitHub Desktop.
Download file server to server
<?php
set_time_limit(0);
file_put_contents( 'progress.txt', '' );
$targetFile = fopen( 'filename.zip', 'w' );
$ch = curl_init( 'URL to File' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
curl_setopt( $ch, CURLOPT_FILE, $targetFile );
curl_exec( $ch );
fclose( $ch );
function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size )
{
static $previousProgress = 0;
if ( $download_size == 0 )
$progress = 0;
else
$progress = round( $downloaded_size * 100 / $download_size );
if ( $progress > $previousProgress)
{
$previousProgress = $progress;
$fp = fopen( 'progress.txt', 'a' );
fputs( $fp, "$progress\n" );
fclose( $fp );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment