-
-
Save stuudmuffin/104fe2f24b3c3d02b21e1fb3d61eeb6a 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 | |
//If output buffer flushing does not work, web-search for reasons your php/webserver setup may need to be adjusted | |
header('X-Accel-Buffering: no'); | |
if (ob_get_level() == 0) ob_start(); | |
//create javascript progress bar | |
echo "<html> | |
<head> | |
<script type='text/javascript'> | |
function updateProgress(perc) { | |
document.getElementById('info').innerHTML = perc + '%'; | |
document.getElementById('prog').value = perc; | |
} | |
</script> | |
</head> | |
<body> | |
<p>Lets see some progress</p> | |
<progress id='prog' value='0' max='100.0'></progress> <span id='info'>0%</span> | |
"; | |
//flush the progress bar | |
ob_flush(); | |
flush(); | |
//Callback Function for later | |
function progressCallback( $resource, $download_size, $downloaded_size, $upload_size, $uploaded_size ) { | |
static $previousProgress = 0; | |
if ( $download_size != 0 ) { | |
$progress = round( $downloaded_size * 100 / $download_size ); | |
if ( $progress > $previousProgress ) { | |
$previousProgress = $progress; | |
//update javacsript progress bar to show download progress | |
echo "<script>updateProgress('$progress');</script>"; | |
ob_flush(); | |
flush(); | |
} | |
} | |
} | |
//Start the curl | |
$targetFile = fopen( 'testfile.iso', 'w' ); | |
$ch = curl_init( 'https://ftp.free.org/mirrors/releases.ubuntu.com/releases/20.04/ubuntu-20.04.3-live-server-amd64.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( $targetFile ); | |
//if we get here, the download has completed | |
echo "<script>document.getElementById('info').innerHTML = 'Done';</script>"; | |
//flush just to be sure | |
ob_flush(); | |
flush(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get your point, ob statements wont work after the DOM is fully rendered.
I am doing this whole process in ajax i.e my curl is executing in ajax.
I am looking for a workaround to get it working like this example, any suggestions ?