Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem serving large (image?) files to Safari
    primarykey
    data
    text
    <p>Server setup: Apache 2.2.14, PHP 5.3.1</p> <p>I use a PHP script to serve files of all types as part of an application with complex access permissions. Things have worked out pretty well so far, but then one of our beta users took a picture with a 10-megapixel digital camera and uploaded it. It's somewhere north of 9 MB, 9785570 bytes.</p> <p>For some reason, in Safari (and thus far ONLY in Safari, I've reproduced this on 5.0.5) the download will sometimes hang partway through and never finish. Safari just keeps on merrily trying to load forever. I can't consistently reproduce the problem - if I reload over and over sometimes the download will complete and sometimes it won't. There's no apparent pattern.</p> <p>I'm monitoring the server access logs and in the cases where Safari hangs I see a 200 response of the appropriate filesize after I navigate away from the page, or cancel the page load, but not before.</p> <p>Here's the code that serves the file, including headers. When the download succeeds and I inspect the headers browser-side I see the content type and size have been set correctly. Is it something in my headers? Something in Safari? Both? </p> <pre><code>header('Content-Type: ' . $fileContentType); header('Content-Disposition: filename=' . basename($fpath)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($fpath)); ob_clean(); flush(); session_write_close(); readfile($fpath); exit; </code></pre> <p>FURTHER BULLETINS AS EVENTS WARRANT:</p> <p>By artificially throttling download speed to 256k/s -- that is, by chunking the file into 256k pieces and pausing between serving them, as</p> <pre><code>$chunksize = 1 * (256 * 1024); // how many bytes per chunk if ($size &gt; $chunksize) { $handle = fopen($fpath, 'rb'); $buffer = ''; while (!feof($handle)) { $buffer = fread($handle, $chunksize); echo $buffer; ob_flush(); flush(); sleep(1); } fclose($handle); } else { readfile($fpath); } </code></pre> <p>I was able to guarantee a successful display of the image file in Safari under arbitrary conditions.</p> <p>A chunksize of 512k does not guarantee a successful display.</p> <p>I am almost certain that the problem here is that Safari's image renderer can't handle data coming in any faster, but:</p> <ul> <li>I would like to know for certain </li> <li>I would also to know if there's some other kind of workaround like a special CSS webkit property or whatever to handle large images because 256k/second is kind of dire for a 10 MB file.</li> </ul> <p>And just to pile on the weird, setting up a finer-grained sleep with usleep() results in problems at a sleep time of 500 ms but not 750 ms.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload