PHP用Socket上传图片

2019-06-24 12:55:23来源: 阅读 ()

新老客户大回馈,云服务器低至5折

服务器端:

<?php
set_time_limit(10);
//* 设置不显示任何错误 */
//error_reporting(0);

function varinfo($str) {
echo "<pre>";
var_dump($str);
echo "<pre>";
}

$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
if ($socket) {
$result = socket_bind($socket, '192.168.3.57', 1337);
if ($result) {
$result = socket_listen($socket, 5);
if ($result) {
echo "监听成功";
}
}
}else{
echo "监听失败";
}

//$path = "/home/upload/";
$path = "e://web//";

while (true){
$connection = socket_accept($socket);
if($connection){
$msg = "telnet succeed!rn";
socket_write($connection, $msg, strlen($msg));

$BufferPond = array();
$buffer = '';

while($flag = socket_recv($connection, $buffer, 1024, 0)){;
if (false !== strpos($buffer,'filename:')){
$filename = substr($buffer,9);
$filename = $path.$filename;
$fp = fopen($filename,"wb");
continue;;
};
fwrite($fp,$buffer);
}
fclose($fp);
socket_close($connection);;
};
};
socket_close($socket);
?>

客户端:
<?php
$errno='001';
$errstr="socket file error";

$fp = fsockopen("192.168.3.57", 1337, $errno, $errstr, 10);
if (!$fp) {;
echo "$errstr ($errno)<br />n";;
} else {;
$str = "filename:socket_file.bmp";;
fwrite($fp, $str);;$out = file_get_contents('../../iphone3G.bmp');;
if($out){
fwrite($fp, $out);;
fwrite($fp,'over');
echo "上传成功";
}

}
fclose($fp);
?>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:PHP用代码实现文件下载

下一篇:PHP代码的优与劣