Situation: User uploads a number of photos via AJAX, then continues interacting with the website whilst a PHP script continues running in the background and generates a variety of thumbnails based on the uploaded photos.
Site config:
- jQuery AJAX (v1.9.1)
- PHP 5.4.7, FastCGI mode
- IIS 7.5, with gzip
Previous posts that I've referred to and tried to implement (but to no avail):
- Closing a connection early with PHP
- PHP: continue after output is complete
- Send AJAX results but continue processing in PHP
- close a connection early
- Disable Gzip compression for single php file with IIS
- http://www.php.net/manual/en/features.connection-handling.php#71172
I have tried a huge number of script options based on the previous posts, however none seem to tell the AJAX script to let the user continue, whilst the PHP continues to process...
Example PHP code:
<?php
// Save images to db, etc
// Now tell AJAX to let the user continue, before generating thumbnails
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off'); // turn IIS gzip for this file
}
ob_end_clean();
header("Connection: close");
header("Content-Encoding: none"); //ensures gzip is not sent through
ob_start();
echo '123'; // 3 digit number will be sent back to AJAX
$size = ob_get_length(); // should mean Content-Length = 3
header("Content-Length: $size");
ob_end_flush();
flush();
ob_end_clean();
// Generate thumbnails, etc
?>
Example jQuery AJAX code:
$.ajax({
type: 'POST',
url: ajax_url,
data: { foo: bar },
beforeSend:function(){
//
},
success:function(data){
alert(data); // Only seems to be firing once the thumbnails have been generated.
}
});
The response headers seem okay...
Question: How do I get AJAX to allow the user to continue once it has received the code from the middle of the PHP script, whilst the PHP script continues to generate the thumbnails?