码迷,mamicode.com
首页 > Web开发 > 详细

php 利用http上传协议(表单提交上传图片 )

时间:2016-04-21 18:27:27      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

  主要就是利用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

php 利用http上传协议(表单提交上传图片 )

标签:

原文地址:http://www.cnblogs.com/loveyouyou616/p/5417818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!