php 利用http上传协议(表单提交上传图片 )
生活随笔
收集整理的這篇文章主要介紹了
php 利用http上传协议(表单提交上传图片 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主要就是利用php 的 fsocketopen 消息傳輸。 這里先通過upload.html 文件提交,利用chrome抓包,可以看到幾個關鍵的信息。
在來看下消息體
#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
總結
以上是生活随笔為你收集整理的php 利用http上传协议(表单提交上传图片 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一站式解决,Android 拍照 图库的
- 下一篇: PHP语言 -- Ajax 查询数据