检测中止写入的字节 [英] detect bytes written on abort

查看:74
本文介绍了检测中止写入的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个高级PHP问题。任何人都可以想到一种方法来检测当脚本中止时写入输出的字节数吗?


我正在向客户端发送一个大文件,我想记录实际发送了多少

字节。我可以使用

关闭处理程序检测脚本的中止。在关机处理程序中,我尝试了ob_get_length,但是

它返回false。我试着读取服务器的日志文件,但是在脚本完全退出之前它确实没有包含这些信息。我试图用
使用fwrite来输出php://然后获取写入的字节返回值。

但是,如果脚本在写入中间中止,那么字节

永远不会被退回。


唯一有用的就是一次写一个字节使用

fread / FWRITE。但这也使得处理器100%加载并且下载速度非常慢(700KB /秒,而使用wget在localhost上为24MB /秒)。


我知道我可以使用后台进程轮询Web服务器的日志文件。

但是如果我可以在脚本中执行所有操作,那么系统将变得更简单,响应更快。


下面是PHP 5.1.1测试脚本。我怀疑它无法完成,但是

任何建议都会受到赞赏!


sendfile.php


ignore_user_abort(false);

set_time_limit(60);

register_shutdown_function(" handleShutdown");


$ fp = false ;

$ fp = fopen(" largefile.html"," rb");

fpassthru($ fp); //脚本在执行中止中止


函数handleShutdown(){

global $ fp;

if($ fp !== false){

fclose($ fp);

}

$ byteswritten = 0; //如何在这里检测?

$ shutdownmessage =

" bytes:"。$ byteswritten。" \ n"。

" status:" .connection_status()。" \ n"。

" aborted:" .connection_aborted()。" \ n";


file_put_contents(" shutdownlog.txt",$ shutdownmessage);

}


这是一个Windows批处理文件来运行请求:


ECHO按Ctrl-C模拟中止

如果EXIST sendfile.php.1 del sendfile.php.1

wget http://localhost/sendfile.php --tries = 1

Here is an advanced PHP question. Can anyone think of a way to detect
the number of bytes written to output when a script is aborted?

I am sending a large file to the client, and I want to record how many
bytes are actually sent. I can detect abort of the script using a
shutdown handler. In the shutdown handler, I tried ob_get_length, but
it returns false. I tried to read the server''s log file, but it is does
not contain the information until the script fully quits. I tried to
use fwrite to php://output, and then get the bytes written return value.
However, if the script aborts in the middle of the write, then bytes
written is never returned.

The only thing that worked was writing one byte at a time using
fread/fwrite. But this also made the processor load 100% and download
speed very slow (700KB/sec versus 24MB/sec on localhost using wget).

I know I can poll the web server''s log file using a background process.
But if I can do everything from within the script, the system becomes
simpler and more responsive.

Below is the PHP 5.1.1 test script. I suspect it can not be done, but
any advice would be appreciated!

sendfile.php

ignore_user_abort(false);
set_time_limit(60);
register_shutdown_function("handleShutdown");

$fp = false;
$fp = fopen("largefile.html", "rb");
fpassthru($fp); // script is aborted in mid-execution

function handleShutdown() {
global $fp;
if($fp !== false) {
fclose($fp);
}
$byteswritten = 0; // how to detect here?
$shutdownmessage =
"bytes: ".$byteswritten."\n".
"status: ".connection_status()."\n".
"aborted: ".connection_aborted()."\n";

file_put_contents("shutdownlog.txt", $shutdownmessage);
}

Here is a windows batch file to run the request:

ECHO Hit Ctrl-C to simulate abort
IF EXIST sendfile.php.1 del sendfile.php.1
wget http://localhost/sendfile.php --tries=1

推荐答案

fp = false;
fp = false;


fp = fopen(" largefile.html"," rb" ;);

fpassthru(
fp = fopen("largefile.html", "rb");
fpassthru(


fp); //脚本在执行中被中止


函数handleShutdown(){

global
fp); // script is aborted in mid-execution

function handleShutdown() {
global


这篇关于检测中止写入的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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