使用curl库上传图片

时间:2011-12-29 12:13:43

标签: php file-upload curl

不是一般性的问题,我用图像上传了一千个,但是我无法处理这个问题。假设问题不是引用者,也不是用户代理,也不是编码,也不是会话等。页面返回的错误是明确的:我知道问题是我无法处理为文件发送“Content-type:image / jpeg”。 这是我通过“实时httpd标题”获得的请求,我正在尝试模拟:

POST /Shared/Webservices/Uploader.ashx HTTP/1.1

Host: ***.it

User-Agent: Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip, deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

DNT: 1

Connection: keep-alive

Referer: ***.it



Content-Type: multipart/form-data; boundary=---------------------------7306436807702238541262456293

Content-Length: 51702

-----------------------------7306436807702238541262456293

Content-Disposition: form-data; name="vert"



au

-----------------------------7306436807702238541262456293

Content-Disposition: form-data; name="userfile"; filename="1.jpg"

Content-Type: image/jpeg


binary dataaaaaaaaaaa

-----------------------------7306436807702238541262456293--

这里有罪的代码:

        $url = $urlwhereuploadto;
        $file = $filepath;

        $headers['Content-Type'] = "multipart/form-data; boundary=AaB03x---------------";
        $headers['Content-Disposition'] = 'form-data; name="userfile"; filename="'.basename($file).'"';

        $data['vert'] = "au";
        $data['userfile'] = "@$file;type=image/jpeg;";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);

        die($result);

任何想法??

1 个答案:

答案 0 :(得分:2)

解决方案:只删除我设置Content-disposition的行。卷发已经设定了它。 (但我总是将它设置在我的其他蜘蛛中......)