Upload file using CURL

Hi Guys!

I am using CURL to post to an form on a different server. The form has the ability to upload a file as well. I have read somewhere that it’s possible to do this using CURL. Here’s the script I am using, but it doesn’t seem to be uploading the file to the other server.

// Start CURL()
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HEADER, 0);
curl_setopt($curl_handle, CURLOPT_VERBOSE, 0);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($curl_handle, CURLOPT_URL, $posturl);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data."&page1=1&doc=@".$_FILES['myfile']['tmp_name']."&newstyle=1");
// Set result var
$page_1_result = curl_exec($curl_handle);

Any ideas?

How can I get the headers of the remote server?

Okay got the boundary in there now. What file data do I need to add? here’s the array so far:

Array ( 
	[0] => POST /create.cgi HTTP/1.1 
    [1] => Content-Type: multipart/form-data 
    [2] => Accept: text/html,application/xhtml+xml,application/xml 
    [3] => Content-Type: application/msword; boundary=---------------------------7a2e4f5dda200487d969d643124935d6 
    [4] => Content-Disposition: form-data; name="myfile"; filename="tattoo.doc" 
    [5] => Content-Length: 24064 ) 

Hi Anthony,

Could you point me in the right direction for what info I need to pass?

Thanks mate

Pop the boundary in there, along with the file data and see how you get on.

…and you’re welcome. :wink:

Okay, I think we are getting somewhere now :slight_smile:

Here’s the headers I am passing now:

$headers = array(
	'POST /create.cgi HTTP/1.1',
	'Content-Type: multipart/form-data',
	'Accept: text/html,application/xhtml+xml,application/xml',
	'Content-Type: '.$_FILES['myfile']['type'],
	'Content-Disposition: form-data; name="myfile"; filename="'.$_FILES['myfile']['name'].'"',
	'Content-Length: '.$_FILES['myfile']['size']);

I managed to get the following error back from CURL:

failed creating formpost data

Does this still mean the headers are not right?

Thanks for your help

to get headers use

curl_setopt($curl_handle, CURLOPT_HEADER, 0);


$url = 'http://www.example.com';
print_r(get_headers($url, 1));

but, you need to make your headers … i think …no need to know remote server headers.

That’s what I thought but it’s not working…

Sure, just use the data submitted in $_FILES to the remote server.

Sure, as stated earlier, you need to replay a ‘normal’ upload. You use PHP to do this.

Here is what I sent to imgftw.net when uploading an image.

POST /Process.php HTTP/1.1
Host: www.imgftw.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv: Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Proxy-Connection: keep-alive
Referer: http://imgftw.net/
Content-Type: multipart/form-data; boundary=---------------------------9466214743445
Content-Length: 66336
Content-Disposition: form-data; name="file[]"; filename="4.jpg"
Content-Type: image/jpeg


Note: content type, content length, the boundaries and other meta data.

Can you see how this is different from what you’re doing?

I have already seen this thread and I guess I need it to do something slightly different.

I have an upload form, which is posting to another upload form on another server (if that makes sense??). The user uploads the file on my site, and it is automatically then submitted to another form on another site. Is this possible?

Look here: http://codingforums.com/showthread.php?t=148555

I just found out this from php.net:

Note: Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded. 

This was probably why the remote server did not recognise the uploaded file before. So I am now passing an array to CURLOPT_POSTFIELDS. I think it’s recognising the file now, because when I pass the $_FILES[‘myfile’][‘tmp_name’] I get a blank page.

Any idea how to debug why I am getting a blank page?

cough :wink:

The code you posted does not send the file data, merely the name.

You need to send the data specifying multipart/form-data, build the content manually. Check the headers and content sent to the remote server when you perform the operation ‘on-site’ so to speak, then use code to reproduce this.

Hi Guys!

Still trying to figure out why I get a blank page when I pass in the $_FILES[‘myfile’][‘tmp_name’].

Any suggestions or ideas?

is $_FILES empty?