PHP用socket上传图片代码
PHP #socket #上传2012-04-22 22:13
服务器端代码:
<?php set_time_limit(10); //* 设置不显示任何错误 */ //error_reporting(0); function varinfo($str) { echo ""; var_dump($str); echo ""; } $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!\r\n"; 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) \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); ?>
相关文章
- PHP关于日/周/月点击统计代码 2012/04/22
- CentOS安装PHP cannot find -lltdl 错误的解决办法 2012/04/20
- PHP中文验证码函数 2012/04/19
- PHP简单实用的分页函数 2012/04/19
- PHP 文件上传脚本 2012/04/19
- PHP验证邮件地址函数 2012/04/19
- PHP防SQL注入漏洞过滤函数 2012/04/19
- php open_basedir用法 安全设置 2012/04/19
- PHP开发安全规则 2012/04/18
- PHP的可变变量名 2012/04/18