伺服器端:
<?php
設定時間限制(10);
//* 設定不顯示任何錯誤 */
//錯誤回報(0);
函數 varinfo($str) {
回顯“<前>”;
var_dump($str);
回顯“<前>”;
$
commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
如果($套接字){
$結果 = socket_bind($socket, '192.168.3.57', 1337);
如果($結果){
$結果 = socket_listen($socket, 5);
如果($結果){
echo "監聽成功";
}
}
}別的{
echo "失敗監聽";
}
//$path = "/home/upload/";
$path = "e://web//";
而(真){
$connection = socket_accept($socket);
如果($連接){
$msg = "telnet成功!rn";
socket_write($connection, $msg, strlen($msg));
$BufferPond = 數組();
$緩衝區='';
while($flag = socket_recv($connection, $buffer, 1024, 0)){
if (false !== strpos($buffer,'檔名:')){
$檔名 = substr($buffer,9);
$檔名=$路徑.$檔名;
$fp = fopen($檔名,"wb");
繼續;
}
fwrite($fp,$buffer);
}
fclose($fp);
socket_close($連接);
}
}
socket_close($socket);
?>
客戶端:
<?php
$errno='001';
$errstr="套接字檔錯誤";
$fp = fsockopen("192.168.3.57", 1337, $errno, $errstr, 10);
如果 (!$fp) {
echo "$errstr ($errno)<br />n";
} 別的 {
$str = "檔案名稱:socket_file.bmp";
fwrite($fp, $str); $out = file_get_contents('../../iphone3G.bmp');
如果($輸出){
fwrite($fp, $out);
fwrite($fp,'結束');
echo "上傳成功";
}
}
fclose($fp);
?>