我目前正在制作一個 PHP 檔案來將多張圖片上傳到圖片服務器上傳。其中接受多部分/表單資料。問題是他們在每個 CURL 請求中只接受最多 10 張圖片。
------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
在一個示例中,我將 20 個請求作為陣列上傳,如下所示:
$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"]}
如果請求包含小于 10 個 IMG 的請求,一切正常,所以如果我上傳 100 個 IMG,我該如何拆分 CURL 10 個請求?
uj5u.com熱心網友回復:
將所有影像放在一個陣列中(無論有多少):
$imgArr = array(
'IMG_00900',
'IMG_00901',
'IMG_00902',
'IMG_00903',
'IMG_00904',
'IMG_00905',
'IMG_00906',
'IMG_00907',
'IMG_00XXX',
...
);
然后我們可以使用array_chunk()將陣列分成多個塊,每個塊最多 10 個元素。
現在我們只需要遍歷塊并一次向它們發送一個塊。
foreach (array_chunk($imgArr, 10) as $imgSet) {
$response = upload($imgSet);
// Do something with the response, if you need to
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/394878.html
