简体   繁体   English

成功下载文件时,PHP服务器与客户端之间的连接断开

[英]lost connection between PHP server and client while downloading a file successfully

This is my code: 这是我的代码:

error_log('download start');
readfile('setup.exe');
error_log('download complete');

The file was received successfully but the third line is not executed, its not log the 'download complete' We facing this problem for a long time we tried also to send chunks (using flush) of this file and after several chunks the connection lost so the rest executable code is not running (but the file received). 该文件已成功接收但执行的第三行,它不记录“下载完成”我们面临这样的问题很长一段时间,我们也试图发送该文件的数据块(使用冲洗),并经过数块的连接丢失等等其余的可执行代码未运行(但已接收文件)。 We know that the connection is lost by using connection handling in php. 我们知道在php中使用连接处理会丢失连接。 We also set connection time limit to max. 我们还将连接时间限制设置为最大。

You need to set certain headers first, binary , etc: 您需要先设置某些标头, binary等:

$file = 'setup.exe';

if (file_exists($file)) {
    error_log('download start');
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    $result = readfile($file);

    if ($result) {
       error_log('download complete');
    }
    else {
       error_log('unable to download');
    }

    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM