Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
JWood Avatar asked Nov 13 '22 04:11

JWood


1 Answers

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');
}
like image 143
Anthony Atkinson Avatar answered Nov 28 '22 01:11

Anthony Atkinson