1

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

user2573863
  • 643
  • 1
  • 9
  • 18
  • What OS are you running XAMPP on? What browser are you testing it with? Because I've run this code on my WAMP server, and tested it with IE and Chrome (on windows), and it pretty much works as advertised. (Except that for some reason my progress function expected only 4 parameters and was missing the resource one) – towr Mar 23 '14 at 15:56
  • It doesn't work in Firefox. (So I'm guessing that's your browser.) – towr Mar 23 '14 at 16:05

1 Answers1

0

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.

sunshinejr
  • 4,834
  • 2
  • 22
  • 32
  • Shouldn't the flushing be preventing that? The flush should tell PHP to output everything up to then. – towr Mar 22 '14 at 22:21
  • Technically yes, but browser are showing all or nothing. So that means ob_flush() or flush() have no impact, because while php scripts are running, no content can be shown. – sunshinejr Mar 22 '14 at 22:25
  • If I do `
    "; ob_flush(); flush();}` I see one extra line appear every second in my browser. So it's neither true that the browser or php wait until the script is complete to output.
    – towr Mar 23 '14 at 15:13
  • http://php.net/flush Read the description, even tho you get the result, it is rare to get it - several servers & browser do not support it. If you want to write code you want it to be working on every browser & server (maybe with exception of IE 6..). – sunshinejr Mar 23 '14 at 15:40
  • Yeah, I read the description and it mentions *netscape*. I think it's safe to say it may not be entirely up-to-date. At the very least this answer is incomplete/inaccurate because it does work in some cases (and as I've tested it on IE and Chrome, I suspect a lot of cases.) – towr Mar 23 '14 at 15:55
  • Tested it in safari & firefox and doesn't give any effect so it depends on the enviroment AND OS AND browser. Too many variables to be relying on. – sunshinejr Mar 23 '14 at 16:05
  • 1
    True. Nice to know a bit more about the problem though :) You'd hope you could pass a header or so to tell the browser to output directly, but I haven't found anything yet. – towr Mar 23 '14 at 16:06
  • If I add `` to the top (as per http://stackoverflow.com/questions/5770917/calling-ob-flush-and-flush-yet-browser-doesnt-show-any-output-until-script) it works for me in Firefox (where it didn't without). Could you see if that also works for you? – towr Mar 23 '14 at 16:12
  • Hey, tested and still the same.. Well, nice thing to discuss anyways. – sunshinejr Mar 23 '14 at 18:47