cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

PHP - Curl and Multipart Form File Upload

From: Andrey Kuznetsov <kandrey89_at_gmail.com>
Date: Thu, 20 Nov 2008 20:10:08 -0800

Hello,
I'm new to PHP but I've spent a lot of time looking and trying.
My goal is to write a php script that would take a file and upload it
to an image host website through the use of multipart form post.

I've read on the php.net site and tried their examples, 1 of them with
fsockopen, that totally didn't work. Now I'm trying curl, and I'm
running into the problem where I get something like this:

* About to connect() to www.imageboo.com port 80 (#0)
* Trying 67.159.45.97... * connected
* Connected to www.imageboo.com (67.159.45.97) port 80 (#0)
> POST /upload.php HTTP/1.1
Host: www.imageboo.com
Accept: */*
Content-Length: 157
Expect: 100-continue
Content-Type: multipart/form-data;
boundary=----------------------------8d605df41713

< HTTP/1.0 200 OK
< Date: Fri, 21 Nov 2008 03:58:28 GMT
< Server: Apache/1.3.41 (Unix) PHP/5.2.5 mod_perl/1.30
mod_log_bytes/1.2 mod_bwlimited/1.4 mod_auth_passthrough/1.8
mod_ssl/2.8.31 OpenSSL/0.9.8b
< X-Powered-By: PHP/5.2.5
< Cache-Control: no-cache, must-revalidate
< Expires: Mon, 26 Jul 1997 05:00:00 GMT
< Content-Type: text/html
< X-Cache: MISS from serv1.submitfly.com
< Via: 1.0 serv1.submitfly.com:80 (squid/2.6.STABLE16)
< Connection: close
* Closing connection #0

The picture.jpg exists, but for some reason the "Content-Type:
multipart/form-data;
boundary=----------------------------8d605df41713" cuts off, I assume
picture data supposed to follow the boudary id?

Here is the code I'm using to try and post the image to this host:

<?php
//HTML form field for multipart/form-data is here
http://www.imageboo.com/index.php
$docurl = curl_init();
curl_setopt($docurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($docurl, CURLOPT_URL, "http://www.imageboo.com/upload.php");
curl_setopt($docurl, CURLOPT_VERBOSE, 1);
curl_setopt($docurl, CURLOPT_HEADER, 1);
curl_setopt($docurl, CURLOPT_POST, 1);
curl_setopt($docurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($docurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($docurl, CURLOPT_POSTFIELDS, array("userfile"=>"{@picture.JPG}"));
$webpage = curl_exec($docurl);
echo "$webpage";
curl_close($docurl);
?>

Here is what I read on mailarchives here, PHP doesn't handle multipart
posts correctly, correct?, instead should use formadd with
CURLOPT_HTTPPOST and CURLFORM_FILE, HOWEVER these commands do not
exist in the PHP interface, so how can I upload images using multipart
form post? What am I doing wrong, has anyone come up with these
solutions or used such methods?

I've spent a couple of days trying to figure this out and so far :(
only switched over to using curl instead of fsockopen, and I don't
know how to use curl very well. It is important that the code be PHP,
because it needs to be run inside or as a script for integration.

Thank you very much.
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-11-21