zoukankan      html  css  js  c++  java
  • php另类上传图片的方法(PHP用Socket上传图片)

    服务器端:

    代码如下:

    <?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! ";
       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 /> ";   
    } 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);
    ?>

  • 相关阅读:
    CSP2020 T1儒略日 暴力模拟90pts代码
    CSP-S 2019 D1T2括号树
    P3593 [POI2015]TAB
    P5145 漂浮的鸭子
    CH0503 奇数码问题
    [NOIP2012]国王游戏 -高精度-贪心-
    费解的开关
    P1040 加分二叉树
    初步学习线段树
    P2758 编辑距离 简单DP
  • 原文地址:https://www.cnblogs.com/lemon66/p/4102736.html
Copyright © 2011-2022 走看看