php 使用进度条下载PHP / cURL文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用进度条下载PHP / cURL文件相关的知识,希望对你有一定的参考价值。

<?php
//output buffer
ob_start();

//create javascript progress bar
echo '<html><head>
<script type="text/javascript">
function updateProgress(percentage) {
    document.getElementById(\'progress\').value = percentage;
}
</script></head><body>

    <progress id="prog" value="0" max="100.0"></progress>
';

//initilize progress bar
ob_flush();
flush();

//save progress to variable instead of a file
$temp_progress = '';
$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( $targetFile );
//must add $resource to the function after a newer php version. Previous comments states php 5.5
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;
        $temp_progress = $progress;
    }
    //update javacsript progress bar to show download progress
	echo '<script>document.getElementById(\'prog\').value = '.$progress.';</script>';
	
	ob_flush();
    flush();
    //sleep(1); // just to see effect
}

//if we get here, the download has completed
echo "Done";

//flush just to be sure
ob_flush();
flush();

?>

以上是关于php 使用进度条下载PHP / cURL文件的主要内容,如果未能解决你的问题,请参考以下文章

PHP 远程文件下载的进度条实现

PHP 下载进度条

如何将 curl 上传进度发送到要显示的 ajax

PHP 使用进度条上传文件 - PHP和JQuery

php上传文件进度条

php 使用进度条上传文件