0

I'm trying to upload a file using curl and php with the following code. I'm passing post data as a string using http_build_query rather than an array because post data is a multi part array. Code works except I can't get image to upload.

$ch = curl_init();
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$image=file_get_contents(realpath('image.jpg'));
    $postFields = array(
         'authenticity_token'=>$token1.'=',
         'menu_item'=>array('name'   => $name,
         'body'=>'',
         'published'=>0,
         'published'=>1,
         'picture'=>$image,
    );


        $postData=http_build_query($postFields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_exec ($ch);
  • Can you post your full code ? – Shankar Narayana Damodaran Apr 07 '14 at 16:22
  • Can you check the answers for these questions mentioned before. [link]http://stackoverflow.com/questions/3433542/curl-php-send-image – Santosh Achari Apr 07 '14 at 16:25
  • And this one. [link]http://stackoverflow.com/questions/16584307/php-upload-an-image-file-to-other-domain-with-curl – Santosh Achari Apr 07 '14 at 16:26
  • Couldn't find an answer to my question in those links... although this comment on the first link might shed some light on my issue: "VolkerK is completely right but my experience suggest that sending a file "@" operator will only work with arrays. $post['file'] = "@FILE_Path" Now you can send the file using CURLOPT_POSTFIELDS" – River city Phoenix Apr 07 '14 at 16:47

2 Answers2

0

First of all make sure what you want to do. If you want to do normal form posting then remove this below header from your code, and it should work.

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));

Now, if you want to do a multipart/form-data posting through curl, then do this:

$postFields = array(
     'authenticity_token'=>$token1.'=',
     'menu_item'=>array('name'   => $name,
     'body'=>'',
     'published'=>0,
     'published'=>1,
     'picture'=> '@' . $image, // better to use path like 'c:/temp/image.jpg'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • You are correct in that removing the header did not stop my code from working... still no image uploaded though. Posting the array without http_build_query doesn't work though and gets me a 400 error as $postFields is a multi-dimensional array. – River city Phoenix Apr 07 '14 at 17:30
  • add this `curl_setopt ($ch, CURLOPT_VERBOSE, 1);` with your code, and show us the Log print by updating your question. – Sabuj Hassan Apr 07 '14 at 17:33
  • curl_setopt ($ch, CURLOPT_VERBOSE, 1); $verbose = fopen('verboselog.txt', 'ab+'); curl_setopt($ch, CURLOPT_STDERR, $verbose); curl_exec ($ch); ---- adding this code is giving me an empty log – River city Phoenix Apr 07 '14 at 18:29
  • where did the `verboselog.txt` come from?? check your apache access log, or error log file for the debug messages! – Sabuj Hassan Apr 07 '14 at 18:31
0

Could not accomplish this using http_build_query on my multi-dimensional post array... the file will not be uploaded by the server. Had to create the array with the nested part as http does...

    $postFields['authenticity_token'=$token1.'=';
    $postFields['menu_item[name]']=$name;        
    $postFields['menu_item[body]']='';
    $postFields['menu_item[published]']=0;
    $postFields['menu_item[published]']=1;
    $postFields['menu_item[picture]']='@'.$image;

curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);