Skip to content

Instantly share code, notes, and snippets.

@valentinemayaki
Forked from bdunogier/curl_progress.php
Created July 31, 2019 16:08
Show Gist options
  • Select an option

  • Save valentinemayaki/55af37f914813dfe561e8f0a8f958012 to your computer and use it in GitHub Desktop.

Select an option

Save valentinemayaki/55af37f914813dfe561e8f0a8f958012 to your computer and use it in GitHub Desktop.

Revisions

  1. @bdunogier bdunogier created this gist Jun 16, 2011.
    32 changes: 32 additions & 0 deletions curl_progress.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php
    file_put_contents( 'progress.txt', '' );

    $targetFile = fopen( 'testfile.iso', 'w' );

    $ch = curl_init( 'http://ftp.free.org/mirrors/releases.ubuntu-fr.org/11.04/ubuntu-11.04-desktop-i386-fr.iso' );
    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 );
    }
    }

    ?>