PHP:上传多张图片到imgur一次 [英] PHP: Uploading multiple images to imgur at once

查看:315
本文介绍了PHP:上传多张图片到imgur一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code和它的作品上传一张图片完全正常使用他们的API来Imgur:

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

不过,我需要一次上传多张图片。在客户端,用户将有多个<输入类型=文件/> 字段。我完全搞清楚,现在我卡住怎么会需要修改此code,以处理多个图片上传当他们通过对数组中的形式传递到服务器。没有人有任何想法?

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?

推荐答案

更改标记如下:

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

现在,你可以通过 $ _ FILES 阵列循环使用的foreach ,就像这样:

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

这篇关于PHP:上传多张图片到imgur一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆