Cannot upload image by PHP CURL

I success when I use command to run

curl -X POST -F [email protected]/myFolder/pic.png -H "Authorization: Bearer tokenX"{accountX}/images/v1

But when I use PHP to curl, code:

// Generated by curl-to-PHP:
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '{accountX}/images/v1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file' => '@' .realpath('/myFolder/pic.png'));

$headers = array();
$headers[] = 'Authorization: Bearer tokenX';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);

CF return error: ERROR 9422: Decode error: image failed to be decoded: Uploaded image must have image/jpeg or image/png content type

My file is png and I can use curl command to post. What did I do wrong?

May I ask, does the upload through the dashboard work for this particular png image? :thinking:

How big the png file is? Less then 10MB?

How about adding:

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);

Are you sure the path to the file is correct?

Which PHP version are you using? Does it have all the needed PHP modules installed?

You might want to try out using

new CURLFile



You might be missing brackets

[ ]

Are you running this code on a server or rather on a local machine like running wia WinNMP or XAMPP?

I also created a question here Cannot upload image by PHP CURL to Cloudflare - Stack Overflow and CURLFile solved the problem.

Thank you so much!

1 Like

Hello my password not work