php - 客户端断开连接时 PHP 脚本是否中止?

标签 php readfile fread seek

我开发了一个网络应用程序内容平台,允许用户搜索和下载(大)视频文件。他们还可以直接在浏览器中观看文件。为了进行一些访问管理,我没有将它们直接指向 mp4/webm 文件,而是使用 PHP 脚本来处理它。我的代码主要基于在 http://www.media-division.com/php-download-script-with-resume-option/ 上找到的示例

片段:

    set_time_limit(0);
    fseek($file, $seek_start);

    while(!feof($file)) 
    {
        print(@fread($file, 1024*8));
        ob_flush();
        flush();
        if (connection_status()!=0) 
        {
            @fclose($file);
            exit;
        }           
    }

它基本上将执行时间设置为无限制(大文件),顺序读取文件并将其推送到客户端。我的问题是:当客户端中止下载时脚本是否中止?例如,当使用 HTML5 视频播放器流式传输视频并停止观看时。

ignore_user_abort 默认情况下是禁用的,我没有在我的配置中启用它。但是 connection_status() 行函数检查连接是否存在,如果不是这种情况则中止脚本(我猜)。

最佳答案

来自documentation :

PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client. Simply using an echo statement does not guarantee that information is sent, see flush().

所以我会说,一旦用户中止,你就调用这两行:

ob_flush();
flush();

脚本将被停止。我不认为这部分会被执行:

if (connection_status()!=0) 
{
    @fclose($file);
    exit;
}

你检查过了吗?有什么理由,为什么你可以假设它不是?

如果您看不到当前进程(共享主机),您可以简单地写一个日志文件来确认

关于php - 客户端断开连接时 PHP 脚本是否中止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27617556/

相关文章:

php - 如果其值已存在,如何从下拉列表中禁用选项

r - 使用 fread() 导入大型 CSV (8 GB) 时“在字符串中嵌入 nul”

r - fread 无法将裸露的 + 或 - 识别为字符字段

php - 通过 php 读取二进制文件并通过 shell 上的 exec 保存它

php - Laravel 5 交响乐\组件\HttpKernel\异常\NotFoundHttpException : POST http://localhost/myproject/public/leads

c - C 中与 Windows 10 的虚拟端口通信

Python - 根据条件读取文本文件中的特定行

c++ - 从文件中读取 (C++)

c - 如何将字符串数组写入和读取到文件?

php - mysql配置: access denied error (1045)