Jump to content

PHP progress bar


HaLo2FrEeEk

Recommended Posts

Ok, I know ths is possible, an example is here:

 

http://www.johnboy.com/php-upload-progress-bar/

 

It uses the ob_flush() and flush() functions of PHP.  From what I've read, these functions have no effect if mod_gzip is enabled in your apache server.  I'm on a shared hosting plan with Dreamhost and they have mod_gzip enabled, but you can request to have it turned off for certain subdomains.  I did so, but it seems that it didn't have an effect.  Here is my code:

 

<?php
ob_start();

for ($i = 0; $i < 5; $i++) {
  echo $i . "<br>";
  ob_end_flush();
  ob_flush();
  flush();
  sleep(1);
  ob_start();
  }

ob_end_flush();
?>

 

This should output numbers from 0 to 5 over 5 seconds, sequentially, WHILE the page is loading.  Instead it just takes 5 seconds for the page to load and then outputs the whole thing at once.

 

I need to know how to make this work.  I have a script that can take 30-45 seconds to run and I want something for my users to look at.  Using Flash really isn't an option because I don't know flash.  I COULD use AJAX and simply send a request to another script, but I'd like to try to avoid anything that requires Javascript.

 

Please, if you guys could help me out, I'd be super grateful!

Link to comment
Share on other sites

That still didn't work.  I just had to wait 20 seconds and all 20 numbers were printed at once.

 

This is weird, because I've seen it done!  I read everywhere that having gzip enabled disrupts this, so I had my host turn off gzip for that domain.  Here are 2 pages that report that gzip is indeed turned off:

 

http://www.whatsmyip.org/http_compression/?url=aHR0cDovL2hhbG8zc2hvdHMuaW5mZWN0aW9uaXN0LmNvbQ==

http://www.port80software.com/tools/compresscheck.asp?url=halo3shots.infectionist.com

 

Both report that this particular subdomain is not being gzipped.  I asked my hosting provider if php's gzip could have something to do with it but they weren't able to answer.  I also asked on another forum and they asked about whether I had APC installed.  I don't know if I have it installed, like I said I'm on a shared hosting plan with Dreamhost, but I'd really like to get this working.  I have a script that can run for up to a minute and I don't want my members wondering what the heck is going on.

 

I tried thinking how I could use AJAX for it, but I can't seem to wrap my head around it.  Basically I'd have to have the main page make an AJAX call for another page which would do part of the work (luckily the script is running a long time because of multiple tasks, not just one), then when that other page finishes I'd send the result back to the server and make another call.  The part I'm not getting is how I'd tell the Javascript to wait until the task is finished to send the next request, everytime I try sending multiple AJAX requests it sends them all at the same time.

 

Either way will work, I'd prefer the PHP method though.

Link to comment
Share on other sites

Web servers are not designed to do what you are trying to do. Even if you get it to work on your current web server/php/browser combination, there is a good chance that it will stop working when either of those three things are changed, due to all the buffering and compression that goes on (your current problem could be due to something at your end of the connection, such as a proxy server your ISP is using on your connection.)

 

If you want to output discrete pieces of information and have each of them displayed 'real time', you must use AJAX to periodically make http requests to a web page and have that page output the current information.

Link to comment
Share on other sites

I understand what you're saying, but I disagree.  I know it's not my browser because every tool that I've used to check whether it's requesting compressed content reports that it indeed is.  I've also checked Firefox with the same results.

 

I know it's not my ISP because of the same thing I said above, how would those pages know I was requesting compressed content if it was being blocked by my ISP?  Also, I can view other pages using this method, such as this one here:

 

http://www.johnboy.com/php-upload-progress-bar/

 

Works just fine in both IE (my main browser) and Firefox.

 

Also, my host tells me that gzip is disabled for this domain, so do all the tools I've used to check.

 

That leaves php, which brings me back to the question I've asked multiple times now.  Could this have something to do with PHP's own gzip capabilities and how would I test it?  How can I disable php's gzip and try this page without it?

 

I just used this page:

 

http://www.port80software.com/products/httpzip/

 

To check the compression on my site.  It turns out that while the root domain is indeed uncompressed, php pages are still being compressed.  I checked with http://halo3shots.infectionist.com and it showed uncompressed, but when I checked http://halo3shots.infectionist.com/progress.php (the page I'm using to test the progress bar) it showed compressed.  So there's my problem, how can I disable php's gzip comprssion?

 

Edit: WAIT.  I must've made a spelling error when I typed in the php page's url and it was reading the error page.  Turns out the progress.php page is actually uncompressed...I'd still like to know how to turn off php gzip compression though.

Link to comment
Share on other sites

I tried it but all that's happening is the script loads completely, waiting through the entire loop, then at the end the progress bar counts up really quickly while it executes all the Javascript.  It doesn't output the buffer contents between each loop.

 

I've contacted Dreamhost and they said that I can create my own local installation of php specific to my account, and I can apply it to a single domain.  My PHP's output_buffering value is set to 4096, so I think I need to send more than 4096 bytes each time for the buffer to actually be output.  I'll do a test and see if it works.

Link to comment
Share on other sites

I'll try it, but I don't see how it's gonna help, we're talking about text here, not images or anything.  I will try though and edit.

 

Edit: Didn't make any difference.

 

So I've created my own php.ini, and I know it's being used because I changed the value of output_buffering from 4096 to Off and the change was reflected in a phpinfo() print.  Is there anything else I should change?  changing the output_buffering setting didn't help.  I've got zlib.output_compression set to Off also.

Link to comment
Share on other sites

Ok, so I found out that if I output more than 64kb to the browser, it'll get sent, so basically all I have to do is output 64kb+ every iteration of my loop.

 

Since that's not feasible, and since I know it's not a browser issue, I've written my host's tech support and they're working with me on the problem.

 

The reason I know it's not a browser thing is because it does the same thing in both Internet Explorer and Firefox.  It does this whether I have output buffering on or off and whether I ob_flush() and flush() or not.

Link to comment
Share on other sites

  • 6 years later...

It seems this topic hasn't been touched in a while, but I had a similar problem and wanted to pass along the solution I found. In spite of turning off output buffers and compression, I found network and browser buffers were getting in the way. All the progress messages were only shown in the final milliseconds on the browser. The solution was to simply fill the pipeline with spaces before the flush. My entire solution that worked was:

 

echo "$i % complete...<br>";

// Padding in case of other browser or network buffers
echo(str_repeat(' ', 4096));

// Now, force this to be immediately displayed:
if (ob_get_level() > 0 && ob_end_flush())
    ob_start();
flush();
Edited by seaware
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.