Alternative method of uploading pictures in php (PHP uses Socket to upload pictures)_PHP tutorial

WBOY
Release: 2016-07-13 10:25:42
Original
830 people have browsed it


Server side:

Copy code The code is as follows:

set_time_limit( 10);
//* Set not to display any errors*/
//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 "Listening successful";
}
}
}else{
echo "Monitoring failed";
}

//$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);
?> ;

Client:

Copy code The code is as follows:
$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 "Upload successful";
}

}
fclose($fp);
?>

http://www.bkjia.com/PHPjc/824956.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824956.htmlTechArticleServer side: Copy the code as follows: ?php set_time_limit(10); //* The setting does not display any errors* //error_reporting(0); function varinfo($str) { echo "PRe"; var_dump($str); ec...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!