Closing an Incoming Connection in Apache / PHP

I have a script that receives large file uploads through a PUT request. These files have some on-the-fly processing when they load. Sometimes we may find that the file is not valid in the first bytes, so we are die()reporting an error. The only problem is that the client is still sending the rest of the data, which is huge waste. Is there a way to disconnect an incoming connection?

the code:

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

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

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

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

+5
source share
1 answer

You can take the following snapshot and see if this completes the connection correctly:

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

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

All Articles