关闭 Apache/PHP 中的传入连接


Closing incoming connection in Apache/PHP

我有一个脚本,它通过PUT请求接收大文件上传。这些文件在上传时会动态完成一些处理。有时我们可以检测到文件在前几个字节中无效,因此我们die()错误消息。唯一的问题是客户端仍然发送其余数据,这是一种巨大的浪费。有没有办法关闭传入连接?

法典:

$fp = fopen('php://input', 'rb');
// Do some data checking here
if( <invalid> ) {
    fclose($fp);
    die('Error');
}

stream_socket_shutdown看起来它可能会完成这项工作,但它没有效果。

有什么办法可以做到这一点吗?即使我必须为此编写扩展?

您可能想试一试以下内容,看看这是否会正确终止连接:

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