質問

I've got the following code and it works perfectly fine for uploading one image to Imgur using their API:

$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); 

However I need to upload multiple images at once. On the client side, the user will have multiple <input type="file" /> fields. I'm completely stuck now with figuring out where and how I will need to modify this code in order to handle multiple image upload when they come through to the server in the form of an array. Does anyone have any ideas?

役に立ちましたか?

解決

Change the markup as follows:

<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>

Now, you can loop through the $_FILES array using a foreach, like so:

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
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top