2017-08-27 202 views
1

我想通过使用PHP的TCP套接字发送.BIN文件。这是我的:通过TCP套接字发送文件

$fp = fsockopen("127.0.0.1", 80, $errno, $errstr, 30); 

if (!$fp) { 
    echo "$errstr ($errno)<br />\n"; 
} else { 
    fwrite($fp, "You message"); 
    while (!feof($fp)) { 
     echo fgets($fp, 128); 
    } 
    fclose($fp); 
} 

我不确定如何发送BIN文件,并在测试时,页面无限循环。

有人可以帮助我吗?有没有更好的方式通过TCP使用PHP发送文件?

+0

你打开一个文件资源(套接字写入),但你的循环看起来像你从另一个读取(推测是bin文件)。使用fopen()打开bin文件,并在循环中将内容写入套接字。 – RichGoldMD

+0

@RichGoldMD我想了解你的意思。你有没有一个编码例子,所以我可以完全理解你想要指出什么? – Mitch

+0

在下面看到我的回答 – RichGoldMD

回答

1

你需要2个文件资源,但是你只打开一个出站:

$fp = fsockopen("127.0.0.1", 80, $errno, $errstr, 30); 
$fb_bin = fopen("myfile.bin", 'rb'); 
// TODO error test $fp_bin 

if (!$fp) { 
    echo "$errstr ($errno)<br />\n"; 
} else { 
    // fwrite($fp, "You message"); 
    while (!feof($fp_bin)) { 
     fwrite($fp, fread($fp_bin, 128)); // ? use a larger value 
     // TODO Error test the read and write operations 
    } 
    fclose($fp); 
    fclose($fp_bin); 
} 
+0

我会试一试,看看我能否进一步调试,当然让你知道。 – Mitch

+0

你有没有运气? – RichGoldMD