程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP如何用Socket上傳圖片(代碼)

PHP如何用Socket上傳圖片(代碼)

編輯: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!\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)<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);
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved