Hi
One More Example to Upload the file with Curl in PHP.
Hello,
I'm trying to POST a file using CURL in PHP.
I've managed to log in the the web site and move through the pages, it saves the cookies and deals with SSL, etc. All this works, but when it comes to uploading a file then it's just not working.
This is what I'm using, the following appears to post the file but it is not accepted by the server on the other side:
[CODE]
<?php
$fields = array (
'field1' => "@$screen1",
'field2' => 'Upload Primary Screenshot',
);
$ch = curl_init($useform);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $userefer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close ($ch);
?>
[/CODE]
This does work, without an upload, and it accepted by the server:
[CODE]
<?php
$fields = http_build_query(array (
'field1' => 'Some normal text',
'field2' => '123456',
));
$ch = curl_init($useform);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $userefer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close ($ch);
?>
[/CODE]
This does not work, it is not accepted by the server:
[CODE]
<?php
$fields = array (
'field1' => 'Some normal text',
'field2' => '123456',
);
$ch = curl_init($useform);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $userefer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close ($ch);
?>
[/CODE]
So you would expect the following to work, but it does not, I assume because the http_build_query function somehow destroys the array containing the file:
[CODE]
<?php
$fields = http_build_query(array (
'field1' => "@$screen1",
'field2' => 'Upload Primary Screenshot',
));
$ch = curl_init($useform);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $userefer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close ($ch);
?>
[/CODE]
I used HttpWatch to monitor exactly what is send when I fill in the form manually, and this is it:
--------------------------
Content-Disposition: form-data; name="field1"; filename="test.jpg"
Content-Type: image/pjpeg
(The image data here)
The server is very strict in the way the POST data is received, so I need to duplicate the browser post exactly. I thought CURL would do that and I don't understand why it does not work.
Please help me work out what the problem is, I've been working on this for days!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thank you for your reply. However, that does not do anything different from the code in my first post, which is not accepted by the server due to the particular way it is posted by the CURL functions.
I need to now how to duplicate the part of the header exactly, which is at the bottom of my previous comment.
Business Accounts
Answer for Membership
by: efg-uaePosted on 2009-08-10 at 12:03:49ID: 25062942
Hi
Please see the code below. i hope it will help you to fix the issue.
Thanks
Malik Asif Joyia
Select allOpen in new window