PHP中以multipart/form-data上传文件流

  •   
  • 1630
  • PHP
  • 0
  • super_dodo
  • 2021/03/21

上传文件流的类封装


class UploadPart
{
protected static $url;
protected static $delimiter;
protected static $instance;
<pre><code>public function __construct() {
    static::$url = 'http://files.note.so/v1/file_server';
    static::$delimiter = uniqid();
}

public function putPart($param) {
    $post_data = static::buildData($param);
    $curl = curl_init(static::$url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        "Content-Type: multipart/form-data; boundary=" . static::$delimiter,
        "Content-Length: " . strlen($post_data)
    ]);
    $response = curl_exec($curl);
    curl_close($curl);
    $info = json_decode($response, true);
    if (!is_array($info['Msg']) &amp;&amp; $info['Msg'] == $param['filesize']) {
        $param['offset'] = $param['filesize'];
        $param['upload'] = '';
        return $this-&gt;putPart($param);
    }
    return $response;
}

private static function buildData($param){
    $data = '';
    $eol = "\r\n";
    $upload = $param['upload'];
    unset($param['upload']);

    foreach ($param as $name =&gt; $content) {
        $data .= "--" . static::$delimiter . "\r\n"
            . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
            . $content . "\r\n";
    }
    // 拼接文件流
    $data .= "--" . static::$delimiter . $eol
        . 'Content-Disposition: form-data; name="upload"; filename="' . $param['filename'] . '"' . "\r\n"
        . 'Content-Type:application/octet-stream'."\r\n\r\n";

    $data .= $upload . "\r\n";
    $data .= "--" . static::$delimiter . "--\r\n";
    return $data;
}

public static function getInstance() {
    if(!static::$instance){
        static::$instance = new static();
    }
   return static::$instance;
}</code></pre>
}

使用方法如下

$fields = array(
'type' =&gt; 'video',
'filename' =&gt; '1407.png',
'filesize' =&gt; 58701,
'offset' =&gt; 0,
'filetype' =&gt; '.acc',
'originName' =&gt; '1407.png',
'upload'=&gt;file_get_contents('0407.png')
);
$part = UploadPart::getInstance()-&gt;putPart($fields);

转载于:https://my.oschina.net/gzheng/blog/3062907