dounai6626 2013-11-14 12:41
浏览 78
已采纳

PHP:一次上传多个图像到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); 

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?

  • 写回答

1条回答 默认 最新

  • duanshang9426 2013-11-14 12:44
    关注

    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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?