0

我有以下代码,它非常适合使用他们的 API 将一张图片上传到 Imgur:

$client_id = $myClientId;
    $file = file_get_contents($_FILES["file"]["tmp_name"]);

    $url = 'https://api.imgur.com/3/image.json';
    $headers = array("Authorization: Client-ID $client_id");
    $pvars = array('image' => base64_encode($file));

    $curl = curl_init();

    curl_setopt_array($curl, array(
       CURLOPT_URL=> $url,
       CURLOPT_TIMEOUT => 30,
       CURLOPT_POST => 1,
       CURLOPT_RETURNTRANSFER => 1,
       CURLOPT_HTTPHEADER => $headers,
       CURLOPT_POSTFIELDS => $pvars
    ));

    $json_returned = curl_exec($curl); // blank response

    $json = json_decode($json_returned, true);

    curl_close ($curl); 

但是我需要一次上传多张图片。在客户端,用户将拥有多个<input type="file" />字段。我现在完全无法弄清楚我需要在哪里以及如何修改此代码,以便在它们以数组的形式到达服务器时处理多个图像上传。有没有人有任何想法?

4

1 回答 1

3

更改标记如下:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="file[]" type="file" multiple="multiple" /><br />
  <input type="submit" value="Send files" />
</form>

现在,您可以使用 a 循环遍历$_FILES数组foreach,如下所示:

foreach ($_FILES['file']['tmp_name'] as $index => $tmpName) {
    if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
    {
        // $tmpName is the file
        // code for sending the image to imgur
    }
}
于 2013-11-14T12:44:42.287 回答