Question

I'm trying to download some file via php curl and show complete persents to the user. I had googled lots of examples, but no one was going to work for me.

For example we will use this code.

echo "<pre>";
echo "Loading ...";

ob_flush();
flush();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);


function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
    if($download_size > 0)
         echo $downloaded / $download_size  * 100;
    ob_flush();
    flush();
    sleep(1); // just to see effect
}

echo "Done";
ob_flush();
flush();

All is in the index.php file. But when i'm trying to execute this file it just loads for a while ( http://take.ms/LlnUv ) and than show me percents. But it should show these percents while downloading file, and not after download because AFTER i don't need that any more.

Loading ...7.5311205259867.53112052598615.19270877173315.19270877173322.8542970174822.8542970174830.51588526322730.51588526322738.17747350897438.17747350897445.83906175472145.83906175472153.50065000046853.50065000046861.16223824621561.16223824621568.82382649196168.82382649196176.48541473770876.48541473770884.14700298345584.14700298345591.80859122920291.80859122920299.47017947494999.470179474949100100100100Done

Was trying to use that ( https://github.com/JokerHacker/CurlAxel ) lib, too. But its not going to work for me, too.

What i'm doing wrong? Thanks

Was it helpful?

Solution

PHP will read the whole output and show it to you after all the scripts are done. Thats why you see everything AFTER the download process. So what can you do? For instance, you can log your progress in a file or database and then by javascript read it on the site. Also, using sleep in your function just slows the whole execution, it's not working as you intended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top