JWood
JWood

Reputation: 2834

Closing incoming connection in Apache/PHP

I've got a script which receives large file uploads via a PUT request. These files have some processing done on the fly as they are uploading. Sometimes we can detect that a file is invalid in the first few bytes so we die() with an error message. The only problem is that the client still sends the rest of the data which is a huge waste. Is there a way to shutdown the incoming connection?

Code:

$fp = fopen('php://input', 'rb');

// Do some data checking here
if( <invalid> ) {
    fclose($fp);
    die('Error');
}

stream_socket_shutdown looked like it might do the job but it has no effect.

Is there any way to do this? Even if I have to write an extension just for this?

Upvotes: 5

Views: 397

Answers (1)

Anthony Atkinson
Anthony Atkinson

Reputation: 3248

You may want to give the following a shot and see if this terminates the connection properly:

$fp = fopen('php://input', 'rb');

// Do some data checking here
if( <invalid> ) {
    fclose($fp);
    header("Content-Length: 0");
    header("Connection: close");
    flush();
    die('Error');
}

Upvotes: 1

Related Questions