Home > Article > Backend Development > How to Properly Post Raw Image Data as Multipart/Form-Data Using cURL in PHP?
Posting Raw Image Data as Multipart/Form-Data in Curl
When attempting to send an image as multipart/form-data using cURL in PHP, you may encounter issues if you are relying on the @$filePath variable within CURLOPT_POSTFIELDS.
Solution
As of PHP 5.6, the use of @$filePath in CURLOPT_POSTFIELDS is deprecated and requires setting CURLOPT_SAFE_UPLOAD. In PHP 7, it is no longer supported. To resolve this issue, you need to utilize a CurlFile object instead.
$fields = [ 'name' => new \CurlFile($filePath, 'image/png', 'filename.png') ]; curl_setopt($resource, CURLOPT_POSTFIELDS, $fields); ````
The above is the detailed content of How to Properly Post Raw Image Data as Multipart/Form-Data Using cURL in PHP?. For more information, please follow other related articles on the PHP Chinese website!