-1

I'm trying to display an image while photos are uploading, so users know my website is doing something instead of just sitting there. I can get it to work properly offline, but not online, for some reason. Here's the relevant part of my code:

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    echo '<div id="progress"><img src="images/ani_hiker.gif"/>
        <p align="center">Photos uploading...<br/>slow and steady!</p></div>';
    ob_end_flush();
    flush();
    ob_flush();
    foreach ($_FILES['files']['name'] as $i => $name) {
          //uploading files junk
    }
    echo '<script language="javascript">
        alert("Photos added successfully!");
        window.location.href="index.php?username='.$profile_link.'&select=photos";
        </script>';

As mentioned before, works perfectly offline, not online. When I try it online, the photos upload, and the div displays after the upload is complete rather than during the upload, and shows up underneath the alert box. The image inside the div does not appear until you've clicked "OK" in the alert box, then flashes for a split second before the div disappears and you're taken to another page (as expected).

I feel like it must be some kind of setting with my server, but I don't know what. I contacted my host company and we disabled output buffering for my entire site, but that didn't fix the problem. I've since resumed output buffering and it's behaving the same as always.

  • disabling output buffering makes the `ob_` functions not do anything... – rm-vanda Apr 07 '15 at 18:50
  • See this answer: http://stackoverflow.com/a/4978642/811240 – Mike Apr 07 '15 at 18:50
  • Can you define what _"works offline"_ means. PHP by nature is usually run on a webserver. Are you referring to your local server?... – War10ck Apr 07 '15 at 18:56
  • By "works offline," I mean when I run it in Xampp on my own laptop (I don't know vocabulary well, local server? virtual server?), it works as expected. – Rob Landauer Apr 07 '15 at 18:58
  • 2
    I can’t see how this is supposed to work at all. When making an HTTP file upload to a PHP script, your receiving script is only _started_ when the upload itself has already finished _completely_ – so trying to show upload progress this way should not work at all, not “locally”, not anywhere else. If you want to do something like this using PHP, then you have to implement it as described in [Session Upload Progress](http://php.net/manual/en/session.upload-progress.php) chapter. – CBroe Apr 07 '15 at 19:00
  • It's supposed to work because the div is echo'd after getting triggered by POST (submitted with a form), but it's written outside the loop where the images are uploaded, and most importantly, written above it, so it should happen first, before the computer even knows an upload will take place. At least, that's the idea. And whether it "should" work or not, it's working offline... Does "receiving script" mean "echo"? Or at least, is that what it means in this case? – Rob Landauer Apr 07 '15 at 19:15
  • 1
    _“At least, that's the idea”_ – yeah, and it’s a completely wrong one. PHP will only start any script that is target of a file upload, when that upload itself has been completed – _all_ data received by the server, temporary files in the upload temp folder have been created, etc. _“it's written outside the loop where the images are uploaded, and most importantly, written above it, so it should happen first”_ – nope, that’s simply not how this works. […] – CBroe Apr 07 '15 at 19:38
  • 1
    […] The files are not “uploaded” inside the loop, they are only moved from the temp folder to a permanent location; again, by that time the upload itself has already finished. You can not output any HTML code “before” that happens, whether it be buffered or not, simply because your script _does not_ start _before_ the uploading bit is done. That this part outputting HTML code is “written above” the part that is supposed to move the files, does not change that general underlaying logic one bit. – CBroe Apr 07 '15 at 19:39
  • OK. I'm wrong. That's why I'm here. Now what should I do about it? How do I get a div to display after a form is submitted, but before the files start uploading? – Rob Landauer Apr 08 '15 at 01:32
  • Either you display it client-side _before_ the actual submit happens – or you implement the Session Upload Progress thing that I already linked to. – CBroe Apr 08 '15 at 05:10

1 Answers1

-1

The desired behavior seems to be something that is and always will be exclusive to your local machine -

From the php.net manual:

As of August 2012, all browsers seem to show an all-or-nothing approach to buffering. In other words, while php is operating, no content can be shown.

In particular this means that the following workarounds listed further down here are ineffective:

1) ob_flush (), flush () in any combination with other output buffering functions;

2) changes to php.ini involving setting output_buffer and/or zlib.output_compression to 0 or Off;

3) setting Apache variables such as "no-gzip" either through apache_setenv () or through entries in .htaccess.

So, until browsers begin to show buffered content again, the tips listed here are moot.

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • This doesn't seem like a browser issue because the OP said it works "offline" (I'm assuming that means on a local web server). – Mike Apr 07 '15 at 18:54