1

I have a script that takes an uploaded text file, parses it and then inserts quite a bit of rows into a database. This process takes about a minute.

Even though I am using AJAX I don't want my users to have to see Loading... for upwards of a minute while the database is being populated. Actually, as soon as the parsing completes successfully, the database INSERTS will always work because everything is validated during parsing.

So what I need to do is return a JSON string {success:true,data:{cartons: 1000, batchname:"Batch1"}} as soon as the parsing is successful. I do this, but the browser doesn't parse the response until the connection is closed (script is through running) Is there anyway to get the browser to accept the response as it comes in or for me to close the connection with the browser while my PHP script continues to run?

I am using ExtJS as the Javascript framework that is handling the AJAX. Any ideas? This has been racking my brain for about a week.

Mike L.
  • 1,936
  • 6
  • 21
  • 37

1 Answers1

1

Just specify the length of the body content of the response:

  $out = 'Return this';
  header("Content-Length: " . strlen($out));
  header("Connection: close");
  echo $out;
  flush();
  echo 'But do not return this';
  // create some file to show that script continues its execution 
  file_put_contents('script.is.running', '');

Also you can run other scripts from command line in the background (without waiting for their completion) or perform a http request to the script with ignore_user_abort function in it.

Cheery
  • 16,063
  • 42
  • 57