Created
June 16, 2011 22:31
-
-
Save bdunogier/1030450 to your computer and use it in GitHub Desktop.
PHP/cURL download progress monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} | |
?> |
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
29.01.2019, script running on php 7.2 with apache2 and ubuntu 18.