views:

77

answers:

1

Hi, 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

A: 

Try this

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");

$bytes = 0;
do
{
    $w = fwrite($fp, substr($buf, $bytes));
    $bytes += $w;
            syslog(LOG_INFO, "written: ".$w);
    if ($w === 0)
    {
        // end of write
        break;
    }
    if ($w === false)
    {
             syslog(LOG_INFO, "error while writing to socket");
             exit();
    }
} while (true);

I believe your while loop was the problem previously, wasn't returning true like it was suppose too etc..

See when it stops writting

Viper_Sb
Thanks for your reply, but I get the same result here. When I print $bytes after the do...while I get something like 11096 as before
Flurin Juvalta
Can you post more of your code, i.e the $fp = fsockopen part? You can edit out the server name etc... but need to see what you're doing as this should work.And for the one I posted, what does it say for "written: xx" lines, more then 1 of them?
Viper_Sb
Ok read your edits and comments now :D, can you post your fsock command, need to see what you're opening and what kind of headers you're sending. again you can strip out anything sensitive.
Viper_Sb