PHP: Uploading multiple images to imgur at once

Allen S

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?

Amal Murali

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple images rename & uploading in PHP

From Dev

Multiple images rename & uploading in PHP

From Dev

Uploading images to IMGUR using Volley returns TimeoutError

From Dev

PHP: Uploading multiple images at the same time

From Dev

Uploading multiple images in webservice using PHP

From Dev

Uploading image using pyimgur to imgur, using images link

From Dev

Uploading multiple images with volley?

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images with Django

From Dev

Uploading multiple images in codeigniter?

From Dev

Django multiple images not uploading

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

Images uploading, never being requested once finished

From Dev

Uploading Multiple Images using CarrierWave

From Dev

All images not uploading in multiple images upload

From Dev

Wont uploading bigger images PHP

From Dev

PHP: Images file is not uploading on the server?

From Dev

PHP foreach($images as $image) not uploading images

From Dev

Enable CORS on multiple images at once

From Dev

Download multiple images at once with Javascript

From Dev

How to comment multiple images at once

From Dev

Enable CORS on multiple images at once

From Dev

Uploading image to Imgur using AngularJS

From Dev

Uploading multiple images from Share Extension

From Dev

Uploading multiple images with other parameters in Swift

From Dev

Django REST: Uploading and serializing multiple images

From Dev

Uploading multiple images to mysql database on Apache server

From Dev

multiple images uploading and converting them to thumbnails in cakephp

Related Related

HotTag

Archive