I'm currently make a PHP file to upload multi images to an image server upload. Which accepted multipart/form-data. Problem is they only accept maximium 10 images on each CURL request.
------WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="image"; filename="IMG_00910"
Content-Type: image/png ------WebKitFormBoundaryXXXXXXXXXXXX
------WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="image"; filename="IMG_00911"
Content-Type: image/png ------WebKitFormBoundaryXXXXXXXXXXXX
------WebKitFormBoundaryXXXXXXXXXXXX
Content-Disposition: form-data; name="image"; filename="IMG_00XXX"
Content-Type: image/png ------WebKitFormBoundaryXXXXXXXXXXXX
In an example, I'm uploading 20 requests as array like this:
$imgArr = array(
'IMG_00900',
'IMG_00901',
'IMG_00902',
'IMG_00903',
'IMG_00904',
'IMG_00905',
'IMG_00906',
'IMG_00907',
'IMG_00XXX'
);
$startUpload = upload($imgArr);
$prin_r($ret);
//{"status":"true","data":["https://example.com/uploaded/IMG_00910","https://example.com/uploaded/IMG_00911","https://example.com/uploaded/IMG_00912","https://example.com/uploaded/IMG_00XX"]}
Everything OK if the request contains smaller than 10 IMGs request, so if I'm upload 100 IMGs, how can I split CURL 10 requests?
CodePudding user response:
Put all images in one array (no matter how many):
$imgArr = array(
'IMG_00900',
'IMG_00901',
'IMG_00902',
'IMG_00903',
'IMG_00904',
'IMG_00905',
'IMG_00906',
'IMG_00907',
'IMG_00XXX',
...
);
Then we can use array_chunk() to split the array into into chunks with max 10 elements each.
Now we only need to iterate through the chunks and send them one chunk at the time.
foreach (array_chunk($imgArr, 10) as $imgSet) {
$response = upload($imgSet);
// Do something with the response, if you need to
}