PHP 的 fwrite 停止写入套接字文件指针
我有以下 PHP 代码,用于写入使用 fsockopen
打开的文件指针 $fp
:
syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");
$bytes = 0;
while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $bytes))))
{
syslog(LOG_INFO, " - " . $w . " bytes written to socket");
$bytes += $w;
}
if ($bytes != strlen($buf))
{
syslog(LOG_INFO, "error while writing to socket");
exit();
}
只要 $buf
的大小,此代码就可以正常工作code> 足够小。大量数据无法完整写入。我得到以下输出:
Write 4900360 bytes to socket:
- 11096 bytes written to socket
error while writing to socket
顺便说一句。 fwrite
的返回值为 0
而不是 false
。
有人知道可能是什么问题吗?非常感谢您的回答,
当删除 fwrite 前面的 @ 时,我收到以下通知:
Notice: fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer in /root/test.php on line 10
Notice: fwrite(): send of 8192 bytes failed with errno=32 Broken pipe in /root/test.php on line 10
我刚刚嗅探了 TCP 流,我发现,我得到了一个
HTTP/1.1 413 Request Entity Too Large
这个问题有解决办法吗?我使用 lighttpd/1.4.22 服务器
I have the following PHP code for writing to a filepointer $fp
that was opened using fsockopen
:
syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");
$bytes = 0;
while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $bytes))))
{
syslog(LOG_INFO, " - " . $w . " bytes written to socket");
$bytes += $w;
}
if ($bytes != strlen($buf))
{
syslog(LOG_INFO, "error while writing to socket");
exit();
}
This code works fine as long as the size of $buf
is small enough. A large amount of data cannot be written completely. I get the following output:
Write 4900360 bytes to socket:
- 11096 bytes written to socket
error while writing to socket
btw. the return value of fwrite
is 0
and not false
.
Does anybody has an idea what could be the problem? Thanks a lot for your answers
I get the following notices when removing the @ in front of the fwrite:
Notice: fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer in /root/test.php on line 10
Notice: fwrite(): send of 8192 bytes failed with errno=32 Broken pipe in /root/test.php on line 10
I just sniffed the TCP Stream and I figured out, that I get a
HTTP/1.1 413 Request Entity Too Large
Is there any fix to this problem? I use a lighttpd/1.4.22 server
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,
我相信你的 while 循环是之前的问题,没有像假设的那样返回 true 等等。
看看它何时停止写入
Try this
I believe your while loop was the problem previously, wasn't returning true like it was suppose too etc..
See when it stops writting