Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Created June 16, 2011 22:31
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 28 You must be signed in to fork a gist
  • Save bdunogier/1030450 to your computer and use it in GitHub Desktop.
Save bdunogier/1030450 to your computer and use it in GitHub Desktop.
PHP/cURL download progress monitoring
<?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 );
}
}
?>
@fa1rid
Copy link

fa1rid commented Dec 6, 2013

Unfortunately, it's not working! Did you test the code?

@abdul202
Copy link

actually it works change this
fclose( $ch );
to thsi
fclose( $targetFile );

@lavakush-rgukt
Copy link

But the file is downloading to server side(File need to be downloaded client side)....and want to show progress bar in browser.Please Help me.!!!

@wilcorrea
Copy link

Very nice, thanks

@TELUS-Alexander
Copy link

Hi,

This is a great script thank you.

When you close the browser the download still continues, this is neat.

@jeffreycahyono
Copy link

PHP 5.5

function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size )

should be

function progressCallback ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size)

@lyarinet
Copy link

File need to be downloaded client side

Copy link

ghost commented Jun 16, 2018

not work... please can you fix it...

@benedictjohannes
Copy link

I have successfully gotten this to work.

progressCallback

As nicely pointed by jeffreycahyono,
I used PHP 7.2, so I (and probably others with PHP 5.5+) changed line 14 to:
function progressCallback ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size)

function order

line no. 9, curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' ) tells Curl that it should look for progressCallback function, that is only defined later. My solution:
Move the progressCallback to before the initiation of Curl (before line6)

fclose

After the two above modification, progress can be monitored in the progress.txt and file downloads perfectly
Meanwhile, I found that in my PHP error log, fclose says that fopen seems to result in null , not file handler.
I tried commenting out the fclose() function, and no more error message, and the file can be deleted with Cpanel on my shared hosting.
Not good practice, I guess, but so far, this is my hack.

Copy link

ghost commented Jan 29, 2019

29.01.2019, script running on php 7.2 with apache2 and ubuntu 18.

<?php
file_put_contents( 'progress.txt', '' );
if(is_file("progress.txt")){ unlink("progress.txt"); }
if(is_file("testfile.iso")){ unlink("testfile.iso"); }
$targetFile = fopen( 'testfile.iso', 'w' );
$ch = curl_init( 'https://speed.hetzner.de/100MB.bin' );
curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );

curl_setopt( $ch, CURLOPT_FILE, $targetFile );
curl_exec( $ch );
fclose( $targetFile );
function progressCallback ($resource, $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 );
    }
}
?>

@vulieumang
Copy link

29.01.2019, script running on php 7.2 with apache2 and ubuntu 18.

<?php
file_put_contents( 'progress.txt', '' );
if(is_file("progress.txt")){ unlink("progress.txt"); }
if(is_file("testfile.iso")){ unlink("testfile.iso"); }
$targetFile = fopen( 'testfile.iso', 'w' );
$ch = curl_init( 'https://speed.hetzner.de/100MB.bin' );
curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );

curl_setopt( $ch, CURLOPT_FILE, $targetFile );
curl_exec( $ch );
fclose( $targetFile );
function progressCallback ($resource, $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 );
    }
}
?>

thank you

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