使用multipart / form-data上传cURL图像

时间:2015-08-31 07:42:06

标签: image curl file-upload multipartform-data boundary

我已经用1个月处理以下代码,使用cURL将图像发布到远程服务器。

    function post_file($url)
    {

    $file_url = "6.jpg"; 
    $eol = "\r\n";   
    $BOUNDARY = '111151173914518';
    $BODY=""; //init my curl body
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY .= 'Content-Disposition: form-data; name="input1"' . $eol . $eol;
    $BODY .= "left" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol;
    $BODY.= 'Content-Disposition: form-data; name="input2"' . $eol . $eol ; 
    $BODY .= "right" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY.= 'Content-Disposition: form-data; name="input3"' . $eol . $eol ;
    $BODY .= "up" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY.= 'Content-Disposition: form-data; name="input4"' . $eol . $eol ;
    $BODY .= "down" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY.= 'Content-Disposition: form-data; name="input5"' . $eol . $eol ;
    $BODY .= "center" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY.= 'Content-Disposition: form-data; name="input6"' . $eol . $eol ;
    $BODY .= "middle" . $eol;
    $BODY.= '-----------------------------'.$BOUNDARY. $eol; 
    $BODY.= 'Content-Disposition: form-data; name="files[]"; filename="6.jpg"' . $eol; 
    $BODY.= 'Content-Type: application/octet-stream'. $eol; /
    $BODY.= 'Content-Transfer-Encoding: multipart/form-data' . $eol . $eol; 

    $BODY.= file_get_contents($file_url) . $eol; 
    $BODY.= '-----------------------------'.$BOUNDARY .'--' . $eol. $eol; 



    $post_file = curl_init(); //init curl      
    curl_setopt($post_file, CURLOPT_HTTPHEADER, array("Content-Type:    multipart/form-data;boundary=---------------------------".$BOUNDARY)  );                 
    curl_setopt($post_file, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($post_file, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($post_file, CURLOPT_TIMEOUT, 40000);
    curl_setopt($post_file, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($post_file, CURLOPT_URL, $url);
    curl_setopt($post_file, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($post_file, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($post_file, CURLOPT_POST, TRUE);
    curl_setopt($post_file, CURLOPT_POSTFIELDS, $BODY);}

我的input1输入6没有问题,但是没有上传图像。

但是当使用cURL上传时,我得到以下结果:

REQUEST HEADERS:

POST /server.php HTTP/1.1
Host: MyServer.com
Accept: */*
Content-Type: multipart/form-data;boundary=-------------------------- -111151173914518
Content-Length: 42987
Expect: 100-continue

RESPONSE:

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/8.5
X-Powered-By: PHP/5.6.10
X-Powered-By: ASP.NET
Date: Mon, 31 Aug 2015 07:12:02 GMT
Content-Length: 171

array (
  'input1' => 'left',
  'input2' => 'right',
  'input3' => 'up',
  'input4' => 'down',
  'input5' => 'center',
  'input6' => 'middle',
) 

除图像外,所有输入都在那里 我试图将内容类型从application / octet-stream更改为image / jpeg 内容传输编码从multipart / form-data到binary和base64,但都无效。

0 个答案:

没有答案