主要就是利用php 的 fsocketopen 消息傳輸。 這里先通過upload.html 文件提交,利用chrome抓包,可以看到幾個關鍵的信息。
首先指定了表單類型為multipart/form-data;。 boundary是分隔符 因為上傳文件不在使用原有的http協議了。請求內容不再可能以 x = y方式發送了。而使用了 分隔符 字段內容 分隔符號 字段內容2 而boundary就是指定分隔符號的標志。 請求的內容就應該是這樣的了。

在來看下消息體

#socket_upload.php 拼接http上傳協議格式 post請求
<?php class SOCKET_UPLOAD { private $host = '127.0.0.1'; private $port = 80; private $errno = null; private $errstr = null; public $timeout = 30; public $url = '/socket/socketupload/upload.php';//請求地址 private $fp = null; private $header = ''; //頭信息 private $body = ''; //消息體 private $boundary = '----abcdefg'; //指定分隔符號的標志 private $res = null; //完整字符串 private $file = null; //文件 private $form = null; //表單 public function __construct($form ='',$file='') { //連接本地80端口 $this->fp = fsockopen($this->host,$this->port,$this->errno,$this->errstr,$this->timeout); if (!$this->fp) exit('failed'); //賦值 $this->form = $form; $this->file = $file; //設置頭信息,消息體 $this->setHead(); $this->setBody(); //拼接整個請求信息 $this->getStr(); } public function write() { //echo $this->res; //寫入 fwrite($this->fp, $this->res); //打印輸出信息 $response = ''; while($row=fread($this->fp, 4096)){ $response .= $row; } fclose($this->fp); $pos = strpos($response, "\r\n\r\n"); $response = substr($response, $pos+4); echo $response; } private function getStr() { $this->header .= "Content-Length:".strlen($this->body)."\r\n"; $this->header .= "Connection: close\r\n\r\n"; $this->res = $this->header.$this->body; } //設置頭信息 private function setHead() { $this->header .= "POST {$this->url} HTTP/1.1\r\n"; $this->header .= "HOST:{$this->host} \r\n"; $this->header .= "Content-Type:multipart/form-data; boundary={$this->boundary}\r\n"; } //設置消息體 private function setBody() { $this->form(); $this->file(); } //非文件表單 private function form() { if ($this->form && is_array($this->form)) { foreach ($this->form as $key=>$val) { $this->body .= "--$this->boundary"."\r\n"; $this->body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n"; $this->body .= "Content-type:text/plain\r\n\r\n"; $this->body .= "{$val}\r\n"; } } } //文件表單 private function file() { if ($this->file && is_array($this->file)) { foreach ($this->file as $key=>$val) { $this->body .= "--$this->boundary"."\r\n"; $this->body .= "Content-Disposition: form-data; name=\"{$val['name']}\"; filename=\"{$val['filename']}\"\r\n"; $this->body .= "Content-Type: {$val['type']}\r\n\r\n"; $this->body .= file_get_contents($val['path'])."\r\n"; $this->body .= "--{$this->boundary}"; } } } } $form = [ 'name'=>'lemon', 'age'=>'12' ]; $file = [ [ 'name'=>'file', 'filename'=>'a.jpg', 'path'=>'a.jpg', 'type'=>'image/jpeg', ] ]; $upload = new SOCKET_UPLOAD($form,$file); $upload->write();
#接收post請求並保存圖片代碼
<?php defined('UPLOAD') or define('UPLOAD',dirname(__FILE__).'/upload'); if ($_FILES['file']['error'] == 0){ $name = $_POST['name']; $age = $_POST['age']; echo 'name is:',$name,"<br/>age is:",$age."<br/>"; $file = $_FILES['file']; $ext = strrchr($file['name'],'.'); $filename = $_SERVER["REQUEST_TIME"].$ext; if (move_uploaded_file($file['tmp_name'],UPLOAD.'/'.$filename)) { echo '<img src="upload/'.$filename.'">'; } }
范例代碼:http://files.cnblogs.com/files/loveyouyou616/socket.zip
