Uploading multiple images using API

Is there any way to upload multiple images to Cloudflare in one API request? I have tried multiple ways of creating a file array but none of them are working. The code I currently have for one image is as follows.

function cloudflare_images_upload( $request ) {
// 	Initialize curl
	$ch = curl_init();
	
// 	Define image to be uploaded (custom image for now, need to update with form data)
	$cfile = curl_file_create(get_stylesheet_directory() . '/includes/php/test.png', 'image/png','test.png');

// 	Define curl options
	curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/accounts/{account-id}/images/v1');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);

// 	Define image in curl request
	$data = array(
		'file' => $cfile
	);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// 	Define headers in curl request
	$headers = array(
		'Authorization: Bearer {api-token}',
		'Content-Type: multipart/form-data'
	);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

	$result = curl_exec($ch);
	curl_close($ch);
	echo 'Done!';
}

I want users to upload images to a form (saved in $request), and then upload these images to Cloudflare. The current code only uploads a certain image that is specified.

1 Like

Or should I loop over all uploaded images and upload them one by one? This would be the only solution I can currently think of, but I was wondering if there was a more elegant way. I’m not sure whether multiple requests for each upload is very time/computationally consuming or not.

This topic was automatically closed after 15 days. New replies are no longer allowed.