Damiano Barbati
Damiano Barbati

Reputation: 3496

Image upload with curl library

Not the generic question, I did one thousand uploads with images but I can't handle this one. Assume the problem is not the referer nor the user agent nor the encoding nor the session and so on. The error the page returns is clear: I know the problem is that I can't handle to send the "Content-type: image/jpeg" for the file. Here is the request I got with "live httpd headers" I'm trying to simulate:

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--

Here the guilty code:

        $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);

Any idea??

Upvotes: 1

Views: 722

Answers (1)

Damiano Barbati
Damiano Barbati

Reputation: 3496

Solution: just strip the line where I set the Content-disposition. Curls already set it. (however i always set it in my other spiders...)

Upvotes: 2

Related Questions