PHP Connection_Aborted()只有时 [英] PHP Connection_Aborted() Works Only Sometimes

查看:127
本文介绍了PHP Connection_Aborted()只有时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件下载PHP脚本。该脚本可以下载大文件最大为4GB +。在现实中,它会经常发生,用户将取消下载进程或将关闭浏览器窗口,同时下载。

I have file download PHP script. The script can download large files up to 4GB+. In reality, it will often happen that users will cancel download process or will close browser window while downloading.

我必须这样做,因此,注册的时候已经开始下载过程将一直为中止任何原因。造成这种情况的最佳解决方案看起来是通过监测 connection_aborted)功能。

I have to, therefore, register when the already started download process will have been for any reason aborted. The optimal solution for this looks to be monitoring connection via connection_aborted() function.

connection_aborted()似乎在下载反应取消或关闭我的浏览器窗口。我的问题是,它不与100%precision反应。它注册取消下载或封闭的浏览器的约50%。如果没有检测到连接中止,下载简单地在服务器上继续,如果浏览器将不取消它。

'connection_aborted()' seems to react on download cancel or closing my browser window. My problem is, that it does not react with 100% precision. It registers about 50% of cancelled downloads or closed browsers. If the connection abort is not detected, the download simply continues on the server as if the browser would not cancel it.

你,请查看我的code的漏洞和错误?我需要了解,是什么原因导致的行为:

Would you, please, review my code for vulnerabilities and errors? I need to understand, what causes the behavior:

// empty and turn off output buffering
    ob_flush();
    flush();


    // never expire this download script
    set_time_limit(0);

    fseek($fileObject, $seek_start);

    while(!feof($fileObject))
    {
        //usleep(100000);

        //print(@fread($fileObject, $chunkSize));
        echo(@fread($fileObject, $chunkSize));

        // gradually output buffer to avoid memory problems by downloading large files
        ob_flush();
        flush();

        // check if the client was disconnected
        // important for cancelled or interrupted downloads
        if (Connection_Aborted())
        {
            ChromePhp::log("Connection Aborted");

            // sent to the database that the connection has been aborted
            $result = mysqli_query($dbc, "UPDATE current_downloads SET connection_aborted=TRUE WHERE user_id=1;");

            // close the database connection
            mysqli_close($dbc);

            // close the open file
            @fclose($fileObject);

            exit(json_encode(array("result" => false, "error" => "Connection with the client was aborted.")));
        }

        $nLoopCounter++;
        $transferred += $chunkSize;
        $downloadPercentage = (($nLoopCounter * $chunkSize) / $fileSize) * 100;

        $result = mysqli_query($dbc, "UPDATE current_downloads SET progress_percent=$downloadPercentage, transferred=$transferred, connection_aborted=$strConnectionAborted, iteration=$nLoopCounter WHERE user_id=1;");
        if($result == false)
        {
            // close the database connection
            mysqli_close($dbc);

            // close the file
            fclose($handle);

            // prepare output message
            $outputArray = array("result" => 0, "message" => "Error Processing Database Query");

            // output the message
            echo json_encode($outputArray);
            exit;
        }
    }

    // file save was a success
    @fclose($fileObject);

我用的是以下内容:


  • 的Apache 2.4.4

  • PHP 5.4.12

  • 的MySQL 5.6.12

  • 谷歌Chrome版本32.0.1700.107米

  • Windows 7的64

感谢您。

推荐答案

添加功能 CONNECTION_STATUS ,然后再试

if(connection_status()!=0||connection_aborted()!=0||){
    //your code
}

这篇关于PHP Connection_Aborted()只有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆