3

so I have tried Show results while script is still executing

but for some reason it doesnt work, so here is what I have so far :

ob_start();
include "../../common.php";

set_time_limit (0);

$start = (string) $_GET['start'];
$end = (string) $_GET['end'];

for($i = $start; strcmp($i, $end); $i = bcadd($i, 1)){

echo $i;
ob_flush();

}

ob_end_flush(); 

UPDATED CODE

*note that this code doesnt work yet!

set_time_limit(0);

$start = $_GET['start'];
$end = $_GET['end'];

for(;$start < $end;$start++){
$content = file_get_contents("[some internal page]");
echo $content;
usleep(10); 
flush();
}
Community
  • 1
  • 1
ahoura
  • 689
  • 1
  • 6
  • 16

3 Answers3

3

Try adding a call to flush() after the call to ob_flush(). This will only work if your server is configured to be able to do it, and does not guarantee that the client will handle it sensibly, but it is your best bet.

One sticking point I have come across here is that is you have zlib.output_compression configured you absolutely cannot do this, full stop. The zlib output compression process is started before any of your code is executed and cannot be controlled by your script at run time with ini_set() and the like.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • I have tried what you said, but its not working... here is my php.ini settings, could it be that : http://s135001.gridserver.com/gs-bin/phpinfo.php5 ??? – ahoura Sep 27 '11 at 14:30
  • hey your script worked , but when I used it with my own piece of code it stopped working!!! :S is there anyway I can talk to you in private? – ahoura Sep 27 '11 at 19:03
  • @ahoura I am about to go to bed (it is 11pm here) but I will be back on in about 11 hours so I will chat then if you want. My guess is that your problem is that you haven't send enough content to the browser for it to start rendering. – DaveRandom Sep 27 '11 at 22:09
  • ok please let me know when you are online so we can finish this up :) thanks alot – ahoura Sep 27 '11 at 22:41
1

You typically need to call both flush() and ob_flush(). See: http://php.net/manual/en/function.flush.php

Also, you cannot do anything about the browser's buffer on the client side. The browser will buffer data as long or as little as it wants. Some servers may also not support flushing their buffer.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I have tried what you said, but its not working... here is my php.ini settings, could it be that : http://s135001.gridserver.com/gs-bin/phpinfo.php5 ??? – ahoura Sep 27 '11 at 14:30
  • 1
    Nothing I can see that would be causing it, but then I don't know everything... try a script with this in it: `"; usleep(10000); flush();}` For me in IE8, it takes about 2-3 seconds before any content is rendered. – DaveRandom Sep 27 '11 at 14:40
  • @ahoura, Post a link to a test script on your server. – Brad Sep 27 '11 at 16:49
  • its wecircled.com/lab/id/imgtest.php I increased the loop to 100000 but still it only shows once the entire thing is done! – ahoura Sep 27 '11 at 18:56
  • @ahoura, On my browser, it works fine. There is nothing wrong with your code. You are experiencing client-side buffering, which is not something you can fix. – Brad Sep 27 '11 at 19:49
  • yea the test works, but when I try it with my code which involves with getting a file using file_get_contents, and then inserting it into DB, and echoing some results – ahoura Sep 27 '11 at 20:32
  • @ahoura, What? Where did you say that in your question? Where is the code posted? Furthermore, what are you trying to accomplish? The concept is the same. I assure you, it works, but I think you are going about this the wrong way. – Brad Sep 27 '11 at 20:35
  • @Brad I have updated the question with the actual code that I am facing a problem with!! I didnt post the entire code because I didnt think it would make a difference!!! Well its sort of a fetcher, the code will be downloading like 1000 pages on each call and I just want to see the results live. I apologize for not posting the actual url,but its a private file that could ruin my server :) – ahoura Sep 27 '11 at 20:50
  • @ahoura, You need to use both `ob_flush()` and `flush()`. – Brad Sep 27 '11 at 20:52
  • I just added `ob_flush()` but still not working... and in the other test file I posted the link, there is no `ob_flush()`... I get the feeling that it has something to do with the stuff inside the loop – ahoura Sep 27 '11 at 21:10
1

After ob_flush(); add flush(); That actually flushes the write buffer and output buffers. ob_flush flushes into the write buffer, flush() then pushes it out to the client. Usually at least.

Wige
  • 3,788
  • 8
  • 37
  • 58
  • I have tried what you said, but its not working... here is my php.ini settings, could it be that : http://s135001.gridserver.com/gs-bin/phpinfo.php5 ??? – ahoura Sep 27 '11 at 14:30