Mark Garcia
Mark Garcia

Reputation: 17708

How do I stop PHP from sending data to client while still running PHP code in server?

This question came up to me when I encountered a bug that caused my PHP program to loop infinitely. Here is an example situation:

Suppose I have a PHP webpage that receives picture uploads (the page perhaps is a response page for an image upload form). In the server, the script should store the image in a temporary file. The script should then output a confirmation message to the client then stop sending data so that the client would not wait. The script should then continue executing, processing the image (like resizing it) before ending.

I think this "technique" could be useful such that the client will not wait during time-consuming processes, therefore preventing time-outs.

Also, could this be solved using HTTP methods?

Upvotes: 6

Views: 3092

Answers (5)

Tony Stark
Tony Stark

Reputation: 8094

I have facing the same problem when i upload image on twitter & facebook from iphone through web service of php.

If the processing time of image upload is not much then you can check the comment of @Musa this may help you but if it takes too much time to process then try this steps.

 1. Image store in folder
 2. Fetch image from folder using cron 
 3. Cron run for every 2 min in backend

these will decrease your processing time.

Hope this help you.

Upvotes: 2

user895378
user895378

Reputation:

Yes.

This can easily be done without any asynchronous processing if you correctly utilize HTTP headers.

Under normal conditions PHP will stop processing as soon as the client on the other end closes the connection. If you want to continue processing after this event, you need to do one thing: tell PHP to ignore user aborts. How?

ignore_user_abort()

This will allow your script to keep running even after the client gets the heck out of dodge. But we're also faced with the problem of how to tell the client that the request they made is finished so that it will close the connection. Normally, PHP transparently handles sending these headers for us if we don't specify them. Here, though, we need to do it explicitly or the client won't know when we want them to stop reading the response.

To do this, we have to send the appropriate HTTP headers to tell the client when to close:

Connection: close
Content-Length: 42

This combination of headers tells the client that once it reads 42 bytes of entity body response that the message is finished and that they should close the connection. There are a couple of consequences to this method:

  1. You have to generate your response BEFORE sending any output because you have to determine its content length size in bytes so you can send the correct header.
  2. You have to actually send these headers BEFORE you echo any output.

So your script might look something like this:

<?php

ignore_user_abort();

// do work to determine the response you want to send ($responseBody)
$contentLength = strlen($responseBody);

header('Connection: close');
header("Content-Length: $contentLength");
flush();

echo $responseBody;

// --- client will now disconnect and you can continue processing here ---

The big "Gotchya" with this method is that when you're running PHP in a web SAPI you can easily run up against the max time limit directive if you do time-consuming processing after the end user client closes the connection. If this is a problem, you may need to consider an asynchronous processing option using cron because there is no time limit when PHP runs in a CLI environment. Alternatively, you could just up the time limit of your scripts in the web environment using set_time_limitdocs.

It's worth mentioning that if you do something like this, you may also want to add a check to connection_aborted()docs while generating your response body so that you can avoid the additional processing if the user aborts before completing the transfer.

Upvotes: 13

Nishant
Nishant

Reputation: 3694

You can use ob_implicit_flush(), It will turn implicit flushing on or off. Implicit flushing will result in a flush operation after every output call, so that explicit calls to flush() will no longer be needed.

refer to

How do i implement this scenario using PHP?

OR

You should Create a standalone cron, which will run after a specific amount of time, and do the in asynchronous way, with out letting the user to know what processing is going on, or with out letting the user to wait. This way you will even be able to detect the failed cases also.

And you should also try to minimize the loading time.

Upvotes: 1

Md Mahbubur Rahman
Md Mahbubur Rahman

Reputation: 2075

You can do it several ways #

1 #

ob_start();
//output
header("Content-Length: ".ob_get_length());
header("Connection: close");
ob_end_flush();
//do other stuff

2 #

Using system() or exec() of PHP, close the Process

3 #

Close the Process using Shell Script

Upvotes: 1

Chris Trahey
Chris Trahey

Reputation: 18290

It is advisable to do these asynchronously. That is, make another script which only processes the previously-created tmp files, and run it with cron (don't even involve apache). When php is running as web-server module, it should be dedicated to quickly forming a response, and then going away to free up resources for the next request.

You are doing the right thing by thinking this way; just keep going one small architectural step further, and fully decouple the request from the heavy lifting that needs to take place.

Upvotes: 1

Related Questions