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:
Previous posts that I've referred to and tried to implement (but to no avail):
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?
If you run request, it will always wait until PHP Script finish executing, or there will be a timeout. So you cannot stop AJAX in middle, and keep PHP running. If you want to upload files, and then create thumbnails, but have info that files are uploaded, do it in two steps:
Thanks to that, thumbs can be also rendered later, when they are first time requested (even without ajax). If you don't want requesting and waiting for thumbs, use cron job on server, which will create thumbs for awaiting images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With