Posting raw image data as multipart/form-data in curl Posting raw image data as multipart/form-data in curl php php

Posting raw image data as multipart/form-data in curl


As of PHP 5.6 @$filePath will not work in CURLOPT_POSTFIELDS without CURLOPT_SAFE_UPLOAD being set and it is completely removed in PHP 7. You will need to use a CurlFile object, RFC here.

$fields = [    'name' => new \CurlFile($filePath, 'image/png', 'filename.png')];curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);


In case anyone had the same problem: check this as @PravinS suggested. I used the exact same code as shown there and it worked for me perfectly.

This is the relevant part of the server code that helped:

if (isset($_POST['btnUpload'])){$url = "URL_PATH of upload.php"; // e.g. http://localhost/myuploader/upload.php // request URL$filename = $_FILES['file']['name'];$filedata = $_FILES['file']['tmp_name'];$filesize = $_FILES['file']['size'];if ($filedata != ''){    $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading    $postfields = array("filedata" => "@$filedata", "filename" => $filename);    $ch = curl_init();    $options = array(        CURLOPT_URL => $url,        CURLOPT_HEADER => true,        CURLOPT_POST => 1,        CURLOPT_HTTPHEADER => $headers,        CURLOPT_POSTFIELDS => $postfields,        CURLOPT_INFILESIZE => $filesize,        CURLOPT_RETURNTRANSFER => true    ); // cURL options    curl_setopt_array($ch, $options);    curl_exec($ch);    if(!curl_errno($ch))    {        $info = curl_getinfo($ch);        if ($info['http_code'] == 200)            $errmsg = "File uploaded successfully";    }    else    {        $errmsg = curl_error($ch);    }    curl_close($ch);}else{    $errmsg = "Please select the file";}}

html form should look something like:

<form action="uploadpost.php" method="post" name="frmUpload" enctype="multipart/form-data"><tr>  <td>Upload</td>  <td align="center">:</td>  <td><input name="file" type="file" id="file"/></td></tr><tr>  <td> </td>  <td align="center"> </td>  <td><input name="btnUpload" type="submit" value="Upload" /></td></tr>


CURL OPERATION BETWEEN SERVER TO SERVER WITHOUT HTML FORM IN PHP USING MULTIPART/FORM-DATA

// files to upload

$filename = "https://example.s3.amazonaws.com/0.jpg";       

// URL to upload to (Destination server)

$url = "https://otherserver/image";

AND

    $curl = curl_init();    curl_setopt_array($curl, array(        CURLOPT_URL => $url,        CURLOPT_RETURNTRANSFER => 1,        CURLOPT_MAXREDIRS => 10,        CURLOPT_TIMEOUT => 30,        //CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,        CURLOPT_CUSTOMREQUEST => "POST",        CURLOPT_POST => 1,        CURLOPT_POSTFIELDS => file_get_contents($filename),        CURLOPT_HTTPHEADER => array(            //"Authorization: Bearer $TOKEN",            "Content-Type: multipart/form-data",            "Content-Length: " . strlen(file_get_contents($filename)),            "API-Key: abcdefghi" //Optional if required        ),    ));   $response = curl_exec($curl);    $info = curl_getinfo($curl);//echo "code: ${info['http_code']}";//print_r($info['request_header']);    var_dump($response);    $err = curl_error($curl);    echo "error";    var_dump($err);    curl_close($curl);